Search code examples
apache.htaccessmod-rewrite

htaccess reverting to php url when taking away www. or https


I have a problem with my htaccess file rewrite rules. I am forcing HTTPS, WWW. and a trailing slash. The redirects work fine, if I remove WWW. or HTTPS then it redirects with them. Nice.

However, if I'm somewhere like "/home/" appended on the url, and I remove either HTTPS or WWW. it will then end up like "//index.php/?module=home" instead of "/home/" appended.

Noticeably, it has the correct behaviour for adding the trailing slash. That does not mess it up.

Full rules:

RewriteRule ^image_prox/([%\w\.-_]*)$ /includes/image_proxy.php?url=$1 [N,NC]

RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

RewriteRule ^mailinglist/$ /index.php?module=mailing_list [N]

RewriteRule ^articles/$ /index.php?module=home [N]

RewriteRule ^all-articles/$ /index.php?module=home&displayall [N,QSA]

RewriteRule ^all-articles/page\=([0-9]+)/$ /index.php?module=home&page=$1&displayall [N]

RewriteRule ^admin/$ /index.php?module=home [L]

RewriteRule ^latest-comments/$ /index.php?module=comments_latest [N]

RewriteRule ^latest-comments/page=([0-9]+)/$ /index.php?module=comments_latest&page=$1 [N,NC,QSA]

RewriteRule ^home/banned/$ /index.php?module=home&message=banned [N]

RewriteRule ^irc/$ /index.php?module=irc [N]

RewriteRule ^register/$ /index.php?module=register [N]

RewriteRule ^about-us/$ /index.php?module=about_us [N]

RewriteRule ^private-messages/$ /index.php?module=messages [N]

RewriteRule ^submit-article/$ /index.php?module=submit_article&view=Submit [N]

RewriteRule ^submit-article/error\=(.+?)?$ /index.php?module=submit_article&view=Submit&error=$1 [N,NC,QSA]

RewriteRule ^contact-us/$ /index.php?module=contact [N]

RewriteRule ^email-us/$ /index.php?module=email_us [N]

RewriteRule ^support-us/$ /index.php?module=support_us [N]

RewriteRule ^articles/$ /index.php?module=home [N]

RewriteRule ^articles/(.+?)\.([0-9]+)/$ /index.php?module=articles_full&title=$1&aid=$2 [N,NC,QSA]

RewriteRule ^articles/([0-9]+)/$ /index.php?module=articles_full&aid=$1 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/error=(.+?)/?$ /index.php?module=articles_full&aid=$2&title=$1&error=$3 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/page\=([0-9]+)/?$ /index.php?module=articles_full&aid=$2&title=$1&page=$3 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/comment_id\=([0-9]+)/?$ /index.php?module=articles_full&aid=$2&title=$1&comment_id=$3 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/article_page\=([0-9]+)/?$ /index.php?module=articles_full&aid=$2&title=$1&article_page=$3 [N,NC,QSA]

RewriteRule ^home/page\=([0-9]+)/$ /index.php?module=home&page=$1 [N]

RewriteRule ^private-messages/page\=([0-9]+)/$ /index.php?module=messages&page=$1 [N,NC,QSA]

RewriteRule ^private-messages/compose/(\d+)*/$ /index.php?module=messages&view=compose [N]

RewriteRule ^private-messages/compose/user\=([0-9]+)/$ /index.php?module=messages&view=compose&user=$1 [N,NC,QSA]

RewriteRule ^private-messages/([0-9]+)/$ /index.php?module=messages&view=message&id=$1 [N,NC,QSA]

RewriteRule ^private-messages/([0-9]+)/page\=([0-9]+)/$ /index.php?module=messages&view=message&id=$1&page=$2 [N,NC,QSA]

RewriteRule ^profiles/([0-9]+)/$ /index.php?module=profile&user_id=$1 [N,NC,QSA]

RewriteRule ^profiles/([0-9]+)/comments/$ /index.php?module=profile&view=more-comments&user_id=$1 [N,NC,QSA]

RewriteRule ^profiles/([0-9]+)/comments/page\=([0-9]+)$ /index.php?module=profile&view=more-comments&user_id=$1&page=$2 [N,NC,QSA]

RewriteRule ^users/statistics/$ /index.php?module=statistics [N,NC,QSA]

RewriteRule ^users/statistics/statid=([0-9]+)/$ /index.php?module=statistics&statid=$1&act=pick [N,NC,QSA]

RewriteRule ^articles/category/(.+?)/page\=([0-9]+)/$ /index.php?module=articles&view=cat&catid=$1&page=$2 [N,NC,QSA,B]

RewriteRule ^articles/category/(.+?)/$ /index.php?module=articles&view=cat&catid=$1 [N,NC,QSA,B]

RewriteRule ^itemdb/([0-9]+)/$ /index.php?module=items_database&view=item&id=$1 [N,NC,QSA]

RewriteRule ^itemdb/developer/([0-9]+)/$ /index.php?module=items_database&view=developer&id=$1 [N,NC,QSA]

RewriteRule ^itemdb/anticheats/([0-9]+)/$ /index.php?module=items_database&view=anticheats&id=$1 [N,NC,QSA]

RewriteRule ^steam-tracker/$ /index.php?module=steam_linux_share [N,NC,QSA]

RewriteRule ^steamplay/$ steamplay.php [N,NC,QSA]

RewriteRule ^steamplay/reports/([0-9]+)/$ steamplay_reports.php?id=$1 [N,NC,QSA]

RewriteRule ^steamplay/reports/([0-9]+)/page=([0-9]+)/$ steamplay_reports.php?id=$1&page=$2 [N,NC,QSA]

RewriteRule ^db/$ itemdb.php [N,NC,QSA]

RewriteRule ^free-games/$ free_games.php [N]

RewriteRule ^itemdb/steamid/([0-9]+)/$ itemdb.php?steamid=$1 [N,NC,QSA]

RewriteRule ^sales/page=([0-9]+)/$ /sales.php?page=$1 [N,NC,QSA]

RewriteRule ^sales/([0-9]+)/$ /sales.php?sale_id=$1 [N,NC,QSA]

RewriteRule ^sales/message=(.+?)/?$ /sales.php?message=$1 [N,NC,QSA]

RewriteRule ^sales/$ sales.php [N]

  Redirect 301 /podcast_rss_ogg.php /podcast_rss.php?format=ogg

  # make sure www. is always there
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  # force trailing slash
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]
  
  # if requested url does not exist pass it as path info to index.php
  RewriteRule ^$ index.php?/ [QSA,L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*) index.php?/$1 [QSA,L]

Solution

  • Your external redirects (non-www to www and HTTP to HTTPS) are in the wrong place. These need to go before the existing rewrites, at the top of the file.

    You will need to clear your browser cache since the erroneous 301 (permanent) redirect will have been cached by the browser. Test first with 302 (temporary) redirects to avoid caching issues.

    (And you need to change those N flags to L.)