【问题标题】:http to https redirection of specific pages with exceptions特定页面的 http 到 https 重定向,但有例外
【发布时间】:2016-10-03 15:09:38
【问题描述】:

我在 .htaccess 文件中实现了以下代码:

RewriteEngine On
Options +FollowSymLinks
RewriteBase /

RewriteCond ${HTTPS} !=on
RewriteRule ^(page1\.php|page2\.php)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

这会将 page1.php 和 page2.php 重定向到 https。但是,我想确保如果用户从 https 页面导航到任何其他页面,他们会转到http://(而不是留在https://)。

我该怎么做?

任何帮助将不胜感激!

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    你可以使用:

    RewriteEngine On
    Options +FollowSymLinks
    RewriteBase /
    
    RewriteCond ${HTTPS} off
    RewriteRule ^(page1\.php|page2\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond ${HTTPS} on
    RewriteRule !^(page1\.php|page2\.php|\.css)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 嗨,谢谢。我已经尝试过实现这一点。但是,当它到达 https 页面时,看起来样式表被忽略了,因为页面根本没有布局。我查看了代码,并且使用相对路径而不是使用 domain.com/css 前缀调用 css。知道为什么会发生这种情况吗?我会继续调查。
    • 是的,这是因为您将 css 重定向到 http。与第二个一起使用:RewriteRule !^(page1\.php|page2\.php|css/) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    • 嗨,谢谢。澄清一下,最后一条指令是:RewriteCond ${HTTPS} on RewriteRule !^(page1\.php|page2\.php|css.)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R =301]
    • 可以,可以添加css目录或者文件类型。我在我的代码中添加了它。
    猜你喜欢
    • 2017-11-15
    • 1970-01-01
    • 2016-09-16
    • 2015-03-19
    • 1970-01-01
    • 2012-11-07
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多