【问题标题】:HTTP to HTTPS Redirect adds both URLHTTP 到 HTTPS 重定向添加了两个 URL
【发布时间】:2019-02-08 02:46:23
【问题描述】:

我在一个主要是自定义的网站上工作,并且 HTTP 没有正确重定向到 HTTPS。

网站加载正常,但 HTTP 版本未正确重定向,因此显示为重复内容。

我已经以几种不同的方式查看了它,但我正在画一个空白。

将 index.php 的 HTTP 请求重写为简单的域名

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index\.php$ https://www.*****.com [R=301,L]

301 重定向 - 如果网页丢失,重定向到 index.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule . index.php [L]

修复规范化问题 - 如果缺少 www,则在 HTTP 请求中添加 www

RewriteCond %{http_host} ^insynergystl.com [nc]
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.insynergystl.com/$1 [r=301,nc] 

始终使用 https 进行安全连接

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.insynergystl.com/$1 [R=301,L]

【问题讨论】:

    标签: php .htaccess redirect https


    【解决方案1】:

    尝试将您的 https 重定向放在 .htaccess 中重定向列表的顶部,或者放在您的虚拟主机配置本身。

    RewriteEngine On
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    

    如果您将日志级别设置得更高一点,您可以调试重定向,只是不要忘记在您测试的部分之后立即将其关闭:

    LogLevel alert rewrite:trace8
    RewriteEngine On
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    LogLevel alert rewrite:trace3
    

    http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging

    在您进行测试时,请确保只发出临时重定向,否则您的浏览器也会缓存重定向,即使代码不再让您这样做,您也会被重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-02
      • 2015-06-05
      • 2020-03-06
      • 2018-04-17
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 2014-12-13
      相关资源
      最近更新 更多