【问题标题】:How can I do a 301 redirect of index.html and force HTTPS?如何对 index.html 进行 301 重定向并强制使用 HTTPS?
【发布时间】:2022-01-09 05:07:50
【问题描述】:

我正在将一个旧的 html 网站移动到 Wordpress。网站结构如下:

  • http://example.com/index.html
  • http://example.com/about.html
  • http://example.com/contact.html

在 WordPress 中它现在看起来像:

  • https://example.com/
  • https://example.com/about/
  • https://example.com/contact/

这是我的.htaccess 文件

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    Redirect 301 /index.html https://example.com/
    Redirect 301 /about.html https://example.com/about/
    Redirect 301 /contact.html https://example.com/contact/
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

(在此处编辑以纠正我遇到的错误) 这有效,除了 http://example.com/index.html 301 重定向到 https://example.com/index.html,然后反复 301 重定向到 https://example.com/ 直到超时。

Redirect 301 /index.html https://example.com/ 无论我将它放在它所在的位置还是在 RewriteRule 行上方都会被忽略。

其他有效的重定向执行从 http://example.com/about.htmlhttps://example.com/about.html 的 301 重定向,然后另一个 301 重定向到 https://example.com/about/ (200 OK)。

如何将http://example.com/index.html 重定向到https://example.com

【问题讨论】:

标签: wordpress .htaccess redirect mod-rewrite


【解决方案1】:

您正在使用 RedirectRewriteRule 。这是两个不同的 Apache 模块,如果混合使用,它们的工作方式会有所不同。您可以使用以下基于RewriteRule 的重定向

RewriteEngine On
RewriteCond %{HTTPS} !=on

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteRule ^index.html$ https://example.com/ [L,R=301]
RewriteRule ^about.html$ https://example.com/about/ [L,R=301]
RewriteRule ^contact.html$ https://example.com/contact/ [L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

确保在测试这些更新的 htaccess 规则之前清除浏览器缓存的数据。

【讨论】:

    【解决方案2】:

    http://example.com/index.html 301 重定向到 https://example.com/index.html 然后反复 301 重定向到 https://onebanquethall.com 直到超时。

    这不应该发生在 Apache 上。 (您在 LiteSpeed 服务器上吗?)如果您尝试从 index.php 重定向(因为您稍后将请求重写为 index.php),这将是一个问题,但 index.html 应该没问题,即使它是分配的DirectoryIndex 文档。

    您可以通过简单地重置脚本顶部的DirectoryIndex 以显式删除index.html 来解决此问题。例如:

    DirectoryIndex index.php
    

    您还可以向该规则添加条件,使其仅重定向来自客户端的初始请求,而不是可能发生的任何内部重写/子请求。

    例如,完整的代码如下所示:

    DirectoryIndex index.php
    
    RewriteEngine On
    
    # Specific redirects
    RewriteCond %{THE_REQUEST} ^GET\s/index\.html
    RewriteRule ^index\.html$ https://example.com/ [L,R=301]
    RewriteRule ^about\.html$ https://example.com/about/ [L,R=301]
    RewriteRule ^contact\.html$ https://example.com/contact/ [L,R=301]
    
    # Redirect HTTP to HTTPS (everything else)
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    # BEGIN WordPress
    : etc.
    

    检查THE_REQUEST 服务器变量的RewriteCond (condition) 指令确保它只会重定向来自客户端的直接请求。 THE_REQUEST 包含 HTTP 请求标头的第一行,如果请求被重写,则不会更改。 RewriteCond 指令仅适用于随后的第一个 RewriteRule 指令。

    “特定”重定向应该在一般 HTTP 到 HTTPS 之前进行,以避免在通过 HTTP 请求旧 URL(这是您正在测试的)时发生双重重定向。

    【讨论】:

    • 谢谢,这很好用。它也可以在没有 DirectoryIndex 行的情况下工作,所以我删除了它。你有什么可以推荐的资源来阅读THE_REQUEST 变量吗?最后,如何检查我的网站是否在 Litespeed 服务器上?再次感谢您,感谢您的帮助。
    • @SamTam 不客气。 “它也可以在没有 DirectoryIndex 行的情况下工作” - 是的,它应该。尽管我的疑问是单独DirectoryIndex index.php 是否可以解决此问题?但是,最好使用 mod_rewrite 并将这些指令定位在 HTTP 到 HTTPS 重定向之上(如所写)。 THE_REQUEST is mentioned in the Apache docs under RewriteCond..
    • @SamTam LiteSpeed 或 Apache - 这可能在 HTTP 响应标头中指示(例如 Server 标头)。或询问您的虚拟主机。 (LiteSpeed 有时会伪装成 Apache,因此可能并不明显。尽管您可以在 .htaccess 中尝试各种指示 LiteSpeed 服务器的指令。)
    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 2012-10-12
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2010-12-21
    相关资源
    最近更新 更多