.htaccess redirect

I want to make all urls go to:

https://www.domain.com/page/var1/var2/ etc. (with all to https and all with trailing slash).

Also, I want index.php to receive all requests, despite the url being /page/variable/variable2 etc.

I have this, but none of it works. I must be doing something wrong.

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]	

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,NS]

Hey there,

First things first, you can most likely get rid of the RewriteBase /. Some hosts require it, but generally it is only used to undo the effects of mod_alias, and since you don’t seem to be using that … :slight_smile:

Okay, if you want everything redirected to https, you should get rid of the first block that redirects to http, that’s just doing work for nothing. Instead build redirection to www in the second rule so it fires if either there request is not for https or the request is for the www-less version of the domain. Also %{HTTPS} is either “on” or no value at all, so matching against “off” just won’t work because it’ll never take on that value (odd though that may seem given that “on” is a valid value …)


RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\\.com$
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [L]	

The second part of your code to let index.php serve everything should work as intended, although /index.php should be replaced with index.php (remove the leading slash) for performance reasons, and some might say . should be replaced with .? but since you index file probably is index.php this is not strictly necessary.

Aw, Rémon, you know better! “RewriteCond %{HTTPS} off” will NEVER evaluate to TRUE (because {HTTPS} is either ‘on’ or null - that’s why I prefer to look at the {SERVER_PORT} and match to ^80$ or ^443$).



I know, that’s why I said:



Sheech! I missed that!