为什么我的 .htaccess 文件不重定向到 https?如何解决?



哦,天哪,我已经尝试过一百万次了,使用Stackoverflow,使用博客,我的虚拟主机甚至一度这样做过,但没有成功,或者可能是我摆弄缓存弄坏了它。

我想把所有的HTTP://流量重定向到https,这会阻止人们购买。

所以这是文件的内容,需要改变什么??

AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
AddHandler server-parsed .html .htm
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilter DEFLATE .shtml
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.pottertour.co.uk/$1 [R,L]
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/html "access plus 1 day"
ExpiresByType text/css "access plus 0 day"
ExpiresByType text/plain "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"  
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES

OK,我试着按照MrWhite的评论删除<IfModule mod_rewrite.c>包装器,它似乎把加载放在一个恒定的循环中。所以我把它放回原来的样子,但是去掉了包装纸。

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^pottertour.co.uk [NC]   
RewriteRule ^(.*)$ https://www.pottertour.co.uk/$1 [L,R=301]   
  • 它现在阻止加载我的谷歌字体和样式。css。非常重要!
  • 重定向失败

感谢解答和解释。

好吧,多亏了white先生,我又试了一次,整个。haccess文件现在是这样的:

AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
AddHandler server-parsed .html .htm
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilter DEFLATE .shtml
</IfModule> 
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/html "access plus 1 day"
ExpiresByType text/css "access plus 10 day"
ExpiresByType text/plain "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"  
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

注意:我删除了所有对永久重定向的引用,省略了R=301

我已经在所有页面上设置了一个规范的https://URL,所以我认为添加到。htaccess并没有多大帮助。

它仍然不能访问我的样式。css或下载谷歌字体。浏览器也没有正确执行页面上的javascript手风琴(多么奇怪的嵌入在页面上!)

我已经联系了我的网络主机,并要求他们确保. haccess文件被"刷新"…我要去调查一下Cloudfare。

快速查看您的站点后,似乎您正在使用Cloudflare(它在应用程序服务器*1之前充当代理),在这种情况下,您需要执行以下操作来将HTTP重定向到HTTPS:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

您不需要<IfModule>包装器。

您应该首先使用302(临时)重定向进行测试,以避免缓存问题,并且只有在确认其正常工作后才更改为301(永久)重定向。301被浏览器持久缓存,因此可能会使测试出现问题。

需要在测试前清除浏览器缓存。

这不会规范化www/非www子域。

*1Cloudflare在应用服务器前充当前端SSL代理。这意味着从客户端到代理的连接是安全的。通过HTTPS(当重定向时),但是从代理到应用服务器的连接总是HTTP。这就是为什么您无法检测到应用程序服务器本身的HTTPS连接,最终会得到一个重定向循环。X-Forwarded-Proto是代理转发的报头集,用于通知后端/应用服务器客户端使用的协议。


更新:我注意到,虽然上面的重定向现在工作,你只是通过Cloudflare发送www子域请求。对域顶点的请求(即。example.com)将直接转到您的原始服务器(Apache),因此不会被上述规则重定向。

所有(或nothing)都应该通过Cloudflare发送,所以这确实是一个DNS配置问题。然而,我们可以通过在之前的.htaccess中实现非www到www的重定向来解决这个问题。(我假设www是首选的规范URL?)

例如:

RewriteEngine On
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS (Cloudflare)
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

注意,之前的规则(HTTP到HTTPS)现在是第二。

这并不假设您没有其他子域。ie。仅example.comwww.example.com。如果您有其他子域,那么您可能会决定在第一条规则中对域进行硬编码(类似于您之前所做的),例如:

# Redirect non-www to www (and HTTPS) - domain hardcoded
RewriteCond %{HTTP_HOST} =example.com
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

相关内容

最新更新