gpt4 book ai didi

apache - 重定向到非 www 并同时执行 SSL 的最佳方法

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:41 24 4
gpt4 key购买 nike

我创建了一种将 www 重定向到非 www 并将任何 http 重定向到 https 的方法。但是,我想知道是否有更优雅、更有效的方法来做到这一点。这是我的代码:

# Force SSL
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

# Rewrite all http to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://{HTTP_HOST}.com/$1 [R=301,L]

最佳答案

您可以使用单个 RewriteRule 强制执行非 www 和 https:

RewriteEngine on

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]

上述 OR 和 AND 条件的组合允许我们一次处理 2 种类型的请求 1) http www, 2) https www 和 规则总是将它们重定向到 ssl 非 www 版本。您可以将 R 替换为 R=301 以使重定向永久化。

关于apache - 重定向到非 www 并同时执行 SSL 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42568095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com