[英]how to redirect https://www.example.com to https://example.com with .htaccess?
www.example.com will redirected to https://example.com . www.example.com将重定向到https://example.com 。
https://www.example.com will not redirected to https://example.com . https://www.example.com不会重定向到https://example.Z4D236D9A2D102C5ZFE6AD1 。 How to do this?
这个怎么做?
The current rules looks like this:当前规则如下所示:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\. [NC]
RewriteCond %{HTTP_HOST} ^(?:[a-z0-9-]+\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.[NC]
RewriteCond %{HTTP_HOST} ^(?:[a-z0-9-]+\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.[NC]
You are missing a space before the flags argument, so it becomes part of the CondPattern and it never matches, so the second redirect block (from HTTPS) never happens.您在 flags 参数之前缺少一个空格,因此它成为CondPattern的一部分并且永远不会匹配,因此第二个重定向块(来自 HTTPS)永远不会发生。
However, this should still be caught by the first rule block, but... this looks like it would create a small redirect loop since the regex ^[a-z0-9-]+\.
但是,这仍然应该被第一个规则块捕获,但是......这看起来会创建一个小的重定向循环,因为正则表达式
^[a-z0-9-]+\.
matches www.
匹配
www.
, but it would also match example.
,但它也会匹配
example.
(after the first redirect). (在第一次重定向之后)。 Potentially redirecting to
https://com/foo
??可能重定向到
https://com/foo
??
You don't need both rules.你不需要两个规则。
Try the following instead:请尝试以下操作:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
You will likely need to clear your browser cache before testing.在测试之前,您可能需要清除浏览器缓存。 (Test with 302 to avoid caching issues.)
(使用 302 进行测试以避免缓存问题。)
But what´s the advantage to do so?
但是这样做有什么好处呢?
In hardcoding the canonical hostname... it depends on whether you have other subdomains or domains pointing to the same account.在对规范主机名进行硬编码时……这取决于您是否有其他子域或指向同一帐户的域。
A hardcoded solution is simpler, less prone to error and generally more reliable.硬编码解决方案更简单,更不容易出错并且通常更可靠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.