HTTP 到 HTTPS 重定向不适用于现有规则

2023-12-02

我已经做了三天了,没有任何结果!

我有一个现有的 http 网站,它有很多重定向规则,具体取决于 URL 友好链接,我现在需要强制加载到 https - Google 最终会将它们从索引中删除,但有很多指向我的第三方网站页面的链接无法物理改变。

下面的 .htaccess 处理http://example.com,但显然不是http://www.example.com

问题是,如果我添加重写并让它专门将 url 前缀更改为 https,它要么根本不起作用,要么将转发到https://www.example.com但随后给出错误消息,因为重定向过多(取决于我尝试过的 http 到 https 的版本)。

我还尝试将代码拆分为首先检查 https/redirect,然后检查非 www,但是当它正确转发时,它会创建一个循环或删除原始查询。

帮助!哈哈

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^example\.co\.uk [NC]
    RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]

    RewriteRule ^2/Home https://www.example.co.uk/  [QSA,L]
    RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?docid=$1&docname=$2 [QSA,L]
    RewriteRule ^item/([^/\.]+)/([^/\.]+)/?$ item.php?prodid=$1&prodname=$2 [QSA,L]
    RewriteRule ^search/ store.php [QSA,L]
    RewriteRule ^store/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ store.php?catid=$1&startPage=$2&limitPerPage=$3&searchTerm=$4 [QSA,L]
    RewriteRule ^store/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ store.php?catid=$1&startPage=$2&limitPerPage=$3 [QSA,L]
    RewriteRule ^store/([^/\.]+)/([^/\.]+)/?$ store.php?catid=$1&catname=$2 [QSA,L]
    RewriteRule ^sitemap\.xml/?$ sitemap.php

    ErrorDocument 404 /15/Error

    AddType application/x-font-woff2 .woff2

    SetEnvIfNoCase User-Agent "^libwww-perl*" block_bad_bots
    Deny from env=block_bad_bots

    #6 month for image files
    <filesMatch ".(jpg|jpeg|png|gif|ico)$">
    Header set Cache-Control "max-age=15552000, public"
    </filesMatch>
    ExpiresActive On
    ExpiresByType image/gif A2592000
    ExpiresByType image/png A2592000
    ExpiresByType image/jpg A2592000
    ExpiresByType image/jpeg A2592000

    # 6 month for css and js
    <filesMatch ".(css|js)$">
    Header set Cache-Control "max-age=15552000, public"
    </filesMatch>

    # long expire
    <filesMatch ".(woff2)$">
    Header set Cache-Control "max-age=102628000, public"
    </filesMatch>

    <IfModule mod_deflate.c>
    #Enable Gzip compression
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font-opentype
    AddOutputFilterByType DEFLATE application/x-font-truetype
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE font/eot
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/vnd.microsoft.icon
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml

    # Remove browser bugs for legacy browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    Header append Vary User-Agent
    </IfModule>

    <IfModule mod_expires.c>
    ExpiresActive On

    # Images
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"

    # Video
    ExpiresByType video/mp4 "access plus 1 year"
    ExpiresByType video/mpeg "access plus 1 year"

    # CSS, JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"

    # Others
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    </IfModule>

您当前没有 HTTP 到 HTTPS 重定向.htaccess对您发布的内容进行编码,因此任何人都可以猜测为什么它对您不起作用。

但是你需要在你的顶部添加类似下面的内容.htaccess,紧接着RewriteBase指示:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

这是“标准”HTTP 到 HTTPS 的重定向。但是,这是否有效(并导致重定向循环)可能取决于服务器上如何管理/实现 HTTPS。例如,您是否使用前端代理(例如 Cloudflare FREE)来管理您的 SSL?

如果您使用前端代理,那么您可能需要将其更改为:

RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

或者,某些共享主机使用HTTPS 环境变量,而不是HTTPS server变量,如第一个示例中使用的:

RewriteCond %{ENV:HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

EDIT: This 相关问题从 2017 年开始,建议与主机“123-reg”使用类似的内容(已收到 123-reg 自己的回复)。他们已经使用了条件模式 !=on,这应该没有什么区别,除非 123-reg 做了一些有点奇怪的事情?!这HTTPS 环境(并且类似地命名server) 变量应包含字符串“off”或“on”。所以,你是否检查并不重要off or !on- 结果should是相同的。

注意:首先使用 302(临时)重定向进行测试,以确保其正常工作 - 这是为了避免任何潜在的缓存问题。在测试之前您需要清除浏览器缓存。

UPDATE:该网站与 123-reg 共享托管

这个 123-reg 支持页面 states:

在我们的 Linux 共享主机帐户上,当连接受 SSL 保护时,将设置环境变量 SSL。

如果是这种情况,那么您应该能够执行以下操作:

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

如果以上内容重定向到 HTTPSSSLenv var 为空(即未设置)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HTTP 到 HTTPS 重定向不适用于现有规则 的相关文章

随机推荐