AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1044319
Accepted
mlclm
mlclm
Asked: 2020-11-30 06:25:18 +0800 CST2020-11-30 06:25:18 +0800 CST 2020-11-30 06:25:18 +0800 CST

HSTS 和 Wordpress 重定向到 www 和非 www 和 https - 避免多次重定向?

  • 772

我正在尝试在我的 Wordpress 网站上实施 HSTS(HTTP 严格传输安全),但没有取得任何成功。首先,我设法将我的网站从非 www 重定向到 www ,包括 https:// ,但我在https://hstspreload.org/上收到消息,它应该首先重定向到 www。

我试图使用 VirtualHosts 配置文件,没有运气。所以我做了一些谷歌搜索,发现这个链接看起来像是 htaccess 的解决方案,但我仍然遇到问题。如果有人知道如何通过 VirtualHost / Apache 配置文件来实现这一点,那就太好了。

错误:HTTP 首先重定向到 www http://inter.net (HTTP) 应在添加 www 子域之前立即重定向到https://inter.net(HTTPS)。现在,第一个重定向是 to https://www.inter.net/。需要额外的重定向以确保任何支持 HSTS 的浏览器都会记录顶级域的 HSTS 条目,而不仅仅是子域。

我的 htaccess 如下:

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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

#### This is what I added : From https://www.danielmorell.com/guides/htaccess-seo/redirects/https-www-and-trailing-slash 
#### Force HTTPS://WWW and remove trailing / from files ####
## Turn on rewrite engine
RewriteEngine on

# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.inter.net/%1 [R=301,L]

# Include trailing slash on directory 
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.inter.net/$1/ [R=301,L]

# Force HTTPS and WWW 
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off  
RewriteRule ^(.*)$ https://www.inter.net/$1 [R=301,L]

# Yoast SEO - XML Sitemap Rewrite Fix
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
RewriteRule ^locations.kml$ /index.php?sitemap=wpseo_local_kml [L]
RewriteRule ^geo_sitemap.xml$ /index.php?sitemap=geo [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]
RewriteRule ^([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-xsl=$1 [L]
# END Yoast SEO - XML Sitemap Rewrite Fix

ps - inter.net 网址仅作为示例。

编辑 - 我已经编辑了我的 example.com.conf 文件,以在下面的答案中添加我的 MrWhite 的额外规则 - 这看起来很准确。运行命令 apachectl configtestSyntaw 后正常。运行service apache2 reload更改以使更改生效,并让所有浏览器都说页面未正确重定向:(**ERR_TOO_MANY_REDIRECTS**每次为每个不同的浏览器清除缓存)。

我只将 htaccess 恢复为原始的 Wordpress 和 Yoast SEO 规则。

我当前在 apache 上为此 VirtualHost 的配置文件可能有问题,但 apachectl configtest 没有语法错误:https ://paste.ofcode.org/vr25hFkPEt2vYjpM5sAUxK

我尝试使用 Firefox Developer 模块(F12)来查看是否可以理解任何其他信息,问题似乎是 301 重定向循环https://www.example.com

编辑2:感谢@MrWhite,我明白ServerAlias细节是不必要的,是循环的原因。问题解决了,从中吸取了教训。

apache-2.2 .htaccess redirect mod-rewrite hsts
  • 1 1 个回答
  • 708 Views

1 个回答

  • Voted
  1. Best Answer
    MrWhite
    2020-11-30T09:17:41+08:002020-11-30T09:17:41+08:00

    总之,HSTS的主要要求是:

    1. 在同一主机上从 HTTP 重定向到 HTTPS 。IE。http://example.com去https://example.com 和 http://www.example.com去https://www.example.com

    2. 仅在 HTTPS 上重定向到规范主机名(www 或非 www)。(即在上面的#1之后)

    3. 仅在 HTTPS 上时发送Strict-Transport-Security(STS) HTTP 响应标头。包括规范重定向(上面的#2)。

      (虽然有几个消息来源指出 STS 标头只能通过 HTTPS 发送,甚至完全无法通过纯 HTTP 发送,但我不认为是这种情况。规范指出,UA 应该在以下情况下简单地忽略此标头通过 HTTP 发送,因此通过 HTTP 发送也不是“问题”。但是,仅通过 HTTPS 发送它并没有太多工作,这就是我在下面实现的方式。)

    因此,这意味着您不必在单个重定向中规范化请求(HTTP / HTTPS / www / non-www),因为这可能违反上面的#1。

    您似乎也没有在您发布的代码中设置 STS 标头。如果您在 Apache(服务器配置或.htaccess)中实现重定向,那么您无法使用 WordPress 设置此标头 - 如果您正在这样做?

    我做了一些谷歌搜索,发现这个链接看起来像是 htaccess 的解决方案

    该“解决方案”没有实施 HSTS。该文章的唯一目的是在单个重定向中规范化请求。该文章顶部的“警告”明确告诉您它违反了 HSTS。

    您还将指令的顺序错误。这些“重定向”指令需要放在WordPress 前端控制器之前,否则它根本不会被 WordPress 的虚拟URL 处理。

    我假设您的规范主机名是www.example.com. (尽管您在问题的标题中提到了重定向到非 www,但您在整个问题的其余部分都重定向到 www?)

    我试图使用 VirtualHosts 配置文件,没有运气。

    尽管可以说在服务器配置中实现这一点更简单、更不容易出错并且更有效(使用单独的虚拟主机)。

    例如(省略“其他”必要指令):

    <VirtualHost *:80>
        ServerName example.com
        # Redirect to HTTPS - same host
        Redirect 301 / https://example.com/
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName www.example.com
        # Redirect to HTTPS - same host
        Redirect 301 / https://www.example.com/
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName example.com
        # Redirect to canonical host (HTTPS only)
        Redirect 301 / https://www.example.com/
    
        # SSL directives...
    
        # Set STS header on the HTTPS redirect ("always" argument is required for this)
        Header always set Strict-Transport-Security "max-age=2592000; includeSubDomains"
    </VirtualHost>
    
    <VirtualHost *:443>
        # Canonical host
        ServerName www.example.com
    
        # SSL directives...
        # etc.    
    
        # Set STS header on the HTTPS response
        Header always set Strict-Transport-Security "max-age=2592000; includeSubDomains"
    </VirtualHost>
    

    注意上面的STS header只设置了max-age1个月的参数,不包含preload参数。如果有意,请务必遵循 HSTS 预加载列表的“部署要求”中给出的说明。https://hstspreload.org/#deployment-recommendations

    或者,要在.htaccess

    (注意:我没有实现“斜杠”重定向,因为您没有在您的要求中提到这一点,它只是从外部文章复制的代码的一部分。)

    # Set HSTS env var only if HTTPS
    RewriteCond %{HTTPS} on
    RewriteRule ^ - [E=HSTS:1]
    
    # Redirect HTTP to HTTPS on the same host
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Redirect non-www to www (HTTPS only)
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Set HSTS header conditionally if request is over HTTPS only (based on HSTS env var)
    Header always set Strict-Transport-Security "max-age=2592000; includeSubDomains" env=HSTS
    
    # Yoast SEO - XML Sitemap Rewrite Fix
    # : (directives go here...)
    # END Yoast SEO - XML Sitemap Rewrite Fix
    
    # BEGIN WordPress
    # : (directives go here...)
    # END WordPress
    

    always指令需要条件,因此Header标头设置为非 200 OK 响应。IE。它需要在非 www 上设置为 www HTTPS 301 重定向。

    另请参阅我对 CodeReview SE 上有关 HSTS 实施的以下问题的回答.htaccess:

    • https://codereview.stackexchange.com/questions/250805/hsts-recommendations-in-htaccess
    • 3

相关问题

  • Apache Django Mod_Wsgi - 自动重新加载应用程序

  • Apache:对多个虚拟主机使用相同的目录指令

  • Apache 上的子域不工作 - 找不到服务器

  • PHP 作为 CGI 还是 Apache 模块?

  • 避免将某些丢失的文件记录到 Apache2 错误日志中

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve