【问题标题】:Why doesn't my .htaccess file redirect to https? How to fix?为什么我的 .htaccess 文件没有重定向到 https?怎么修?
【发布时间】:2021-11-18 01:52:25
【问题描述】:

天哪,我已经尝试过一百万次了,使用 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

好的,我尝试按照 MrWhite 的评论删除 &lt;IfModule mod_rewrite.c&gt; 包装器,它似乎将加载置于一个恒定循环中。所以我把它恢复到以前的样子,但没有包装。如下所示。

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^pottertour.co.uk [NC]   
RewriteRule ^(.*)$ https://www.pottertour.co.uk/$1 [L,R=301]   
  • 它现在阻止加载我的 google 字体和 styles.css。 真的很重要!
  • 它也无法重定向。

非常感谢解决方案和解释。

好的,感谢 MrWhite,我又尝试了一次,整个 .htacess 文件现在位于:

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 并没有多大帮助。

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

我已联系 1&1 我的网络托管服务商,并要求他们确保 .htacess 文件已“刷新”...我会去研究 Cloudfare。

【问题讨论】:

  • “我的网络主机甚至曾经做过” - 你的“网络主机”确实应该知道一种可行的方式,因为这可能取决于主机(如果他们有前端缓存代理等) .您上面的指令是一种完全有效的方法,那么会发生什么?没有?一个错误?重定向循环?您应该删除 &lt;IfModule mod_rewrite.c&gt; 包装器。
  • potertour.co.uk 是您的实际域吗?这甚至没有回答我的 DNS 请求(即它没有解析)?!
  • 是的,你说得对,这是错误的 URL,应该是 www.pottertour.co.uk 而不是从 .htacess 粘贴,而是从我粘贴到 .htacess 的博客教程中粘贴。经过多次尝试后,我想最终解决这个问题,但我以某种方式阻止了 styles.css,从而使我的整个网站脱轨!耶稣!我已经尝试按照您的建议删除包装器,但它会将其置于一个恒定循环中。所以在恐慌中我把它改回原来的样子,减去包装器,它仍然无法重定向到 https,但现在它也阻止了 styles.css 和我的谷歌字体 - 噩梦!
  • 我非常怀疑“耶稣”是否参与其中。总的来说:宗教错觉在信息学中确实没有立足之地......
  • 不过,也许我确实需要我的styles.css 才能起死回生,所以那里有一两件事要学。你能成为我的救世主@askascha 吗?没有十字架 - 承诺。

标签: .htaccess http https


【解决方案1】:

不幸的是,您的编辑/cmets 并没有真正的帮助。但是,快速浏览您的站点后,您似乎正在使用 Cloudflare(它在您的应用程序服务器前充当代理),在这种情况下,您需要执行以下操作来将 HTTP 重定向到 HTTPS:

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

您不需要 &lt;IfModule&gt; 包装器。

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

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

这不会规范 www/non-www 子域。


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

Everything(或nothing)应该通过 Cloudflare 发送,所以这确实是一个 DNS 配置问题。但是,我们可以通过在.htaccess 上述 HTTP 到 HTTPS 重定向中实现非 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]

【讨论】:

  • 感谢 MrWhite 的支持,我已经做到了,尽管切换 [R=301, L] for [R,L],我清除了浏览器缓存 {'cookies & other site data' & 'Cached images & files'},不改变。我尝试了 Chrome 的隐身模式 - 这真的会强制重新加载所有内容吗?仍然没有 style.css 加载。此外,如果我粘贴 pottertour.co.uk,它不会重定向。真的有点崩溃,我会失去预订。
  • 完全奇怪,非重定向 HTTP 版本显示样式和字体并且可以完全工作,而 HTTPS 版本没有。哇!我们有一个异常队长。
  • 好神奇,现在它似乎可以正常工作,完全重定向,谢谢 MrWhite,你是冠军! ?️ 我已经通过从相对路径更改为在线 URL 来修复 style.css 不加载问题;不过让我感到难过,因为离线更新有点困难。额外的步骤......叹息?
  • “我尝试了 Chrome 的隐身模式 - 这真的会强制重新加载所有内容吗?” - 仅当它是 new 隐身会话时。如果您已经打开了一个隐身窗口,那么它不会。重定向本身不应该影响样式加载。当我查看您的网站时,样式加载正常,但我只查看了主页。 “从相对路径更改” - 使用静态资源的相对路径肯定会导致问题,特别是在您重写 URL 时(尽管在您的 .htaccess 文件中没有证据)。请改用以斜杠开头的相对根 URL。
  • @Sam“使离线更新更难” - 您仍然应该使用网络服务器(Apache)“本地”开发,以便您可以保持 URL 路径与实时站点相同。 (如果您使用的是绝对 URL,那就有点棘手了。)“请注意,我通过省略 R=301 删除了对永久重定向的所有引用” - 澄清一下,302/临时重定向仅用于测试目的。这最终应该是一个 301 永久重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多