开发者

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?


  1. RewriteRule just matches the path section of the url, so it needs to be just ^index\.php$.
  2. The query string can be matched using RewriteCond %{QUERY_STRING}.
  3. Like in aesphere's answer, testing for the special %{HTTPS} variable is easier than checking the port.
  4. Using an absolute target URI implies [R=301,L] so you can remove it.
  5. [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}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜