【问题标题】:How to redirect a single page from https to http如何将单个页面从 https 重定向到 http
【发布时间】:2018-03-19 17:01:22
【问题描述】:

我已将我的网站重定向到 https,但我需要将单个页面从 https 重定向到 http。

我重定向到 https 的规则是:

  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我可以使用什么规则将单个页面重定向到 http?

【问题讨论】:

  • 这个问题被问了一遍又一遍。事实证明,最好的办法是解决为什么您要重定向的原因,而不是实际使您的服务不安全或损坏。
  • @arkascha 因为在此页面中我需要使用不适用于 ssl 的外部小部件
  • 是的,包括外部内容是此类尝试的常见原因。重定向不起作用的位,浏览器将拒绝遵循这样的重定向。您需要通过您的服务代理该资源。

标签: apache .htaccess redirect


【解决方案1】:

我查看了几年前的一个类似问题的答案。此代码根据 cmets 工作:

尝试使用 %{SERVER_PORT}(如果默认 http 端口仍为 80 而 ssl 端口为 443)

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/blog_rss\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^(blog_rss\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]

原始问题/答案在:301 redirect HTTPS to HTTP for a single page

【讨论】:

  • 这不适用于嵌入在页面中的外部小部件。所有浏览器都会出于非常充分的理由拒绝遵循这样的重定向。
【解决方案2】:

首先你的代码不会捕获任何wwww

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# the first two lines above will catch all http request so , nothing will 
# pass to the following 
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

让你的代码像这样:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^   https://www.yoursite.com%{REQUEST_URI} [L,R=301]

排除特定页面添加此

 RewriteCond %{HTTPS} off [OR]
 RewriteCond %{HTTP_HOST} !^www\.
 RewriteCond %{REQUEST_URI} !^/your/path/to/pages$ 
 RewriteRule ^   https://www.yoursite.com%{REQUEST_URI} [L,R=301]

注意:清除浏览器缓存并进行测试。

更新:

 RewriteCond %{HTTPS} on
 RewriteCond %{THE_REQUEST} my-page [NC] 
 RewriteRule ^   http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

试试这个,这意味着捕获任何包含https & 你的页面的请求然后强制进入http

【讨论】:

  • 对不起,我在 %{REQUEST_URI} 之前错过了 %,现在试试吧
  • 还是不行。我使用 Drupal github.com/drupal/drupal/blob/7.x/.htaccess 并且我已经按照 drupalaid.com/blog/how-to-quickly-add-ssl-to-your-drupal-site 添加 https 重定向
  • 但您的原始代码仍然会强制任何 http 进入 https,无论 www 是否存在
  • 相信我应该可以尝试将所有文​​件保存在外部并清除浏览器缓存,然后仅放置强制 https 的代码,然后放置我的代码。你的文件在主根目录还是在哪里给出路径?
  • 感谢您的帮助,但仍然无法正常工作。这是我在主根目录中的 .htaccess:pastebin.com/MJAJU9ar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 2013-04-15
相关资源
最近更新 更多