我知道这是一个常见的话题,但我已尽最大努力通过我在网上搜索的解决方案来解决这个问题。
我们有一个链接到子域名secure.mysite.com的证书,我们希望实现以下目标:
我们需要在以下路径及其子页面强制使用SSL:
http://mysite.com/services [/ ... / ...] => https://secure.mysite.com/services [/ ... / ...]
http://mysite.com/login [/ ... / ...] => https://secure.mysite.com/login [/ ... / ...]
http://mysite.com/member [/ ... / ...] => https://secure.mysite.com/member [/ ... / ...]
http://mysite.com/admin [/ ... / ...] => https://secure.mysite.com/admin
我们想在所有非安全路径上添加www(除上述路径外的所有路径)
我们希望删除所有安全路径上的www(上面提到的所有路径)
以下是我在.htaccess文件中添加的内容,但没有成功:
# force HTTPS
RewriteCond %{HTTPS} =off
RewriteRule ^(services|login|member|admin)$ https://secure.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force HTTP
RewriteCond %{HTTPS} =on
RewriteRule !^(services|login|member|admin)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# remove www from host when HTTPS
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]
# add www from host when HTTP
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Zend redirect logic
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
答案 0 :(得分:2)
进行一些更正:
RewriteEngine On
# force HTTPS
RewriteCond %{HTTPS} =off
RewriteRule ^(services|login|member|admin) https://secure.mysite.com%{REQUEST_URI} [L,R=301]
# force HTTP
RewriteCond %{HTTPS} =on
RewriteRule !^(services|login|member|admin) http://mysite.com%{REQUEST_URI} [L,R=301]
# remove www from host when HTTPS
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
# add www from host when HTTP
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} !^(www|secure)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]