【问题标题】:htaccess redirect to ssl only for certain pages and www non secure for the other pageshtaccess 仅针对某些页面重定向到 ssl,对于其他页面,www 不安全
【发布时间】:2012-08-28 22:56:09
【问题描述】:

我需要将网站的所有请求重定向到 www,只有某些页面重定向到 https 而没有 www,因为出于某种原因,我们客户购买的 ssl 证书不包括 www。 环顾四周后,我设法完成了大部分工作。但是,一旦我们访问了一个安全的 url 页面,其他页面将停留在 https 上。 这是我目前在.htaccess 中的内容:

#Redirect to www
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Force SSL on checkout login account and admin pages
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} checkout|login|my-account|administrator|webshop
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L

如果 https 已打开且不属于列出的 url,我会错过将重定向到 www 不安全的部分。但我对正则表达式和重写规则一点也不熟悉。 一些帮助将不胜感激。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect ssl


    【解决方案1】:

    尝试使用此代码:

    # Force SSL on checkout login account and admin pages
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} checkout|login|my-account|administrator|webshop
    RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
    RewriteRule ^(.*)$ https://%2/$1 [R=301,L,QSA]
    
    # Remove SSL on other pages
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} !checkout|login|my-account|administrator|webshop
    RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
    RewriteRule ^(.*)$ http://www.%2/$1 [R=301,L,QSA]
    
    # Force www for non https
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
    

    【讨论】:

    • 非常感谢您的帮助。但是 SSL 指令会导致安全 url 只出现在主页上。可能是 Joomla 自己的重写规则与这些规则冲突造成的?
    • 是的,它可能会发生冲突,还要确保这些规则是您的 htaccess 中的第一个。
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 2020-09-26
    • 2011-12-12
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多