Force SSL / https:// using .htaccess for a Content Management System (CMS)
Content Management Systems often force pages to go though an index.php route.
For ex开发者_JS百科ample pages on the CMS I use are presented in the URL as follows.
http://www.my-domain.co.uk/index.php?page=263
The .htaccess code I know only listens to the index.php part of my statement.
My current re-write code stands as follows;
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^login/?$ https://www.my-domain.co.uk/login [R=301,L]
This works as it is a specific directory, however,
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^index.php?page=263$ https://www.my-domain.co.uk/index.php?page=263 [R=301,L]
does not seem to work.
Any ideas?
- RewriteRule just matches the
path
section of the url, so it needs to be just^index\.php$
. - The query string can be matched using
RewriteCond %{QUERY_STRING}
. - Like in aesphere's answer, testing for the special
%{HTTPS}
variable is easier than checking the port. - Using an absolute target URI implies
[R=301,L]
so you can remove it. - [QSA] appends the query string
So, you end up with:
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} ^\?page=263$
RewriteRule ^index\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA]
I have a snippet I copied from a webpage somewhere. Perhaps you could give it a try?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
精彩评论