This question is about:
- Forcing HTTPS
- Keep/make the URL tidy
- Catch all calls to be directed to a routing file
Before I had SSL on my site, I had a RewriteRule like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /directory/file.php?url=$1 [NC,L,QSA]
which forced all requests to go to the file.php in /directory/ but the URL looked like (which was lovely):
http://example.com
so that all the /argument/argument2/argument3 etc could be added after the URL to be picked up as parameters in file.php and be handled, like this:
$args = array_filter(
explode("/", htmlspecialchars($_GET["url"])), function($value) { return $value !== ''; }
);
Then I added this:
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/directory/file.php?url=$1 [NC,L,QSA]
in hope that the URL should look like this and point to the same file:
https://www.example.com/
but now the URL looks like this:
https://www.example.com/directory/file.php?url=index.html
It did not just change to https and added the /directory/file.php?url=, it also added index.html to the URL.
The full rewrite looks like this all together:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/directory/file.php?url=$1 [NC,L,QSA]
Edited:
So i fixed it by doing
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(.*)$" "/directory/file.php?url=$1" [NC,L,QSA]