【问题标题】:How to redirect from HTTPS to HTTP?如何从 HTTPS 重定向到 HTTP?
【发布时间】:2016-09-02 11:08:33
【问题描述】:

我有一个网站。我需要将所有页面从 HTTP 重定向到 HTTPS。但是有两个页面不应该通过 HTTPS 提供。

  • 主页 - www.hellomysite.com
  • 经销商页面 -www.hellomysite.com/dealers

即使用户输入的 url 为 https://www.hellomysite.com/dealers,它也应该通过 HTTP 提供。 http://www.hellomysite.com/dealers

我用谷歌搜索并找到了许多链接,但没有一个被重定向。

.htaccess

  #Redirect all request to HTTPS
  RewriteCond %{HTTPS} off [OR]
  RewriteCond %{HTTP_HOST} ^www.hellomysite\.com*
  RewriteRule ^(.*)$ https://hellomysite.com/$1 [L,R=301]


  #RewriteCond %{HTTPS}  on [OR]
  #RewriteRule  ^https://hellomysite.com/dealers$   http://hellomysite/dealers [R=301,L,QSA]

如果我再尝试其他方法,那么在打开网站时会出错

这个网站有太多的重定向

如何将主页和经销商页面重定向到 HTTP。

【问题讨论】:

    标签: php .htaccess drupal drupal-6


    【解决方案1】:

    试试:

      #Redirect all request to HTTPS
      RewriteCond %{HTTPS} off [OR]
      RewriteCond %{HTTP_HOST} ^www\.
      RewriteRule !^$|dealer https://hellomysite.com%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 不,另外我得到一个错误。如果我输入 www.hellomysite.com - 404 Page Not FOund
    • 是404吗?确保目标网址存在。尝试清除浏览器缓存
    • 没有。 .htaccess 中的配置错误,行号指向规则
    • @Aarti 我发布的规则在我的服务器上运行良好。我可以将所有 http 请求(除了 root 和 /dealer)重定向到 https。
    • 我重新检查过,确实如此,但由于它在浏览器的 url 中附加了一个长查询字符串,如 ~2621934.2876410/htdocs/dev/index.php?q=products
    【解决方案2】:

    如果我理解你,下面的代码会解决它:

    RewriteEngine On
    RewriteCond %{HTTPS} =off
    
    RewriteCond %{SCRIPT_FILENAME} !\/index\.php [NC]
    #the above line will exclude https://www.hellomysite.com/index.php
    # from the following rules
    
    RewriteCond %{SCRIPT_FILENAME} !\/dealers\.php [NC]
    # the above line will exclude the https://www.hellomysite.com/dealers.php
    # from the following rules
    
    RewriteRule (.+) https://%{HTTP_HOST}/$1 [L,R=301]
    # above line will force every pages and directories ,except those who 
    # excluded above and the main root , to redirect from http to https
    # (.+) means not to consider http://www.hellomysite.com/ and if you
    # change it by  (.*) it will be considered 
    

    现在您可以强制整个网站从 http 重定向到 https,除了 www.hellomysite.com 和 www.hellomysite.com/dealers。

    注意:请确保在测试上述代码之前清空浏览器缓存

    【讨论】:

    • Nopes.. 它将每个页面重定向到 HTTP,仅供参考,我没有 index.php 或任何带有 .php 扩展名的页面。我正在使用 Drupal
    • 我正在使用 Chrome Icongite 窗口来检查所有这些
    猜你喜欢
    • 1970-01-01
    • 2019-09-10
    • 2017-10-06
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2016-01-17
    • 2011-09-28
    相关资源
    最近更新 更多