开发者

force ssl with .htaccess while rewriting addresses

I am currently using this .htaccess to redirect all the requests for pages with a directory to my index.php

RewriteEngine on
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ /seattle/index.php/$1 [L]

And this works just fine and produces urls that hide the index.php, and I have code in index.php that makes urls clean looking.

But now I need to force pages to connect via ssl, so I tried

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ https://example.com开发者_如何学C/seattle/index.php/$1 [L]

and it works to force ssl, but now it also forces the url to include the index.php:

https://example.com/seattle/index.php/pagename

instead of what I want

https://example.com/seattle/pagename

What am I missing?


To change protocol (HTTP -> HTTPS) and/or domain name (www.example.com -> example.com) the proper redirect ("301 Permanent Redirect" or "302 Found/Temp Redirect") is required.

Therefore you cannot combine rewrite and redirect and still showing original URL. It has to be 2 different rules and the one for changing protocol/domain should be listed first. For example:

RewriteEngine on

# force HTTPS for all URLs
RewriteCond %{HTTPS} =off
RewriteRule . https://example.com%{REQUEST_URI} [R=301,L]

# other rewrite rules
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ /seattle/index.php/$1 [L]

The rule I have added will redirect ALL HTTP URLs to HTTPS. If you need only some of them to be redirected -- add appropriate conditions via additional RewriteCond line(s).

The %{HTTPS} is the most common and kind of "proper" way of checking if SSL is ON or OFF (but it is all depending on your specific circumstances and server config). When checking against %{HTTPS} you are safe against situation when your site is running on non-standard port (other than 80). You can use %{SERVER_PORT} =80 instead (will work for majority of cases).

With the above rules the rewrite for http://example.com/seattle/pagename will occur in 2 steps:

  1. 301 Redirect to https://example.com/seattle/pagename
  2. Rewrite (internal redirect) to /seattle/index.php/pagename
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜