HTTP到HTTPS重定向不使用现有规则 [英] HTTP to HTTPS redirect not working with existing rules

查看:6
本文介绍了HTTP到HTTPS重定向不使用现有规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试了3天了,还是没有结果!

我有一个现有的http网站,它有很多重定向规则,依赖于URL友好链接,我现在需要强制加载到HTTPS-谷歌最终会从索引中删除它们,但有很多从第三方网站到页面的链接,我不能实际更改。

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

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

我还尝试将代码拆分,首先检查https/reDirect,然后检查非www,但当它正确转发时,它要么创建一个循环,要么剥离原始查询。

救命!LOL

    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 MSIE !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>

HTTPS

您当前在发布的.htaccess代码中没有推荐答案到HTTPS的重定向,所以任何人都不知道它为什么对您无效。

但您需要在.htaccess的顶部紧跟RewriteBase指令:

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

这是"标准"的HTTP到HTTPS重定向。然而,这是否有效(并导致重定向循环)可能取决于HTTPS在服务器上的管理/实现方式。例如,您是否使用前端代理(例如,免费的CloudFlare)来管理您的SSL?

如果您使用的是前端代理,则可能需要将其更改为以下内容:

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

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

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

编辑:这篇来自2017年的related question建议与上面类似的内容用于主持人"123-reg"(已收到123-reg自己的回复)。他们使用了CondPattern!=on,这应该不会有什么不同,除非123-reg做了一些有点奇怪的事情?!HTTPS环境(以及类似名称的服务器)变量应包含字符串"off"或"on"。因此,您是否选中off!on并不重要-结果相同。

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

更新:该网站使用123-reg共享主机

This 123-reg support page状态:

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

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

RewriteCond %{ENV:SSL} ^$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
如果环境变量为空(即.未设置)。

这篇关于HTTP到HTTPS重定向不使用现有规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆