【问题标题】:Force HTTP for all pages except specific URLs对除特定 URL 之外的所有页面强制使用 HTTP
【发布时间】:2016-05-11 23:33:25
【问题描述】:

我有一种情况,我需要强制我网站中的每个页面都重定向到 HTTP,但需要强制重定向到 HTTPS 的两个特定 URL 除外。

需要重定向到HTTPS页面的两个页面是:

/microsoft-moc-on-demand-video-training/moc-registration-page/

/课程/注册/

我在 .htaccess 文件中使用的代码如下所示:

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(/courses/register/|/microsoft-moc-on-demand-video-training/moc-registration-page/)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

不幸的是,这似乎不起作用。整个站点确实重定向到 HTTP(因此部分代码有效),但是这两个异常(应该重定向到 HTTPS)并没有这样做,它们保持为 HTTP 链接。

知道我在这里做错了什么吗?

【问题讨论】:

  • 只是一个相关的问题:为什么不将所有内容重定向到 https?如果您操纵个人资料或信用卡信息,我强烈建议您在整个网站上使用https,以免出错...

标签: .htaccess mod-rewrite https


【解决方案1】:

问题是对于RewriteRule,请求的路径中没有初始/。因此,您正在尝试匹配不存在的内容。

在第一个捕获组中每个选项的末尾还有一个额外的 /,当与最后的 / 结合使用时,需要一个路径,例如 /courses/register//

以下代码应该适合您的需要:

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(courses/register|microsoft-moc-on-demand-video-training/moc-registration-page)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

【讨论】:

    猜你喜欢
    • 2014-01-22
    • 2010-11-14
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2018-03-14
    • 2017-04-07
    相关资源
    最近更新 更多