Reputation: 69
We would like to force all requests to our website to use the HTTPS protocol. We just want to replace the protocol of the URL, the rest of the URI can stay the same. Everything works when we start browsing through the website from the homepage. When we open any other page that isn't the homepage first (i.e. ourdomain.com/this-is-a-page/) we don't get redirected to use HTTPS. What do I need to change on my htaccess file to accomplish this?
This works (it inserts https): ourdomain.com
This doesn't work: ourdomain.com/this-is-a-page/
Thanks!
htaccess code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Upvotes: 2
Views: 2240
Reputation: 143966
You need to put your redirect rule before your routing rule. The rewrite engine loops so the routing rule (which routes stuff to index.php
) gets executed first, the rewrite engine loops, then the second redirect rule gets applied, which redirects the wrong thing. Try:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Upvotes: 5