简体   繁体   English

how to redirect https://www.example.com to https://example.com with .htaccess?

[英]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.

相关问题 htaccess重定向https:// www。 (https://www.example.com)到非www(https://example.com) - htaccess redirect https://www. (https://www.example.com) to non www (https://example.com) 将https://example.com重定向到https://www.example.com - Redirect https://example.com to https://www.example.com 将https://www.example.com重定向到https://example.com - Redirect https://www.example.com to https://example.com 将www.example.com和https://www.example.com重定向到https://example.com - Redirect www.example.com and https://www.example.com into https://example.com htaccess将http://example.com重写为http://www.example.com,但将https://www.example.com重写为https://example.com - htaccess rewrite http://example.com to http://www.example.com but https://www.example.com to https://example.com htaccess将example.com/about重定向到https://www.example.com/about - htaccess redirect example.com/about to https://www.example.com/about 解析 http://www.example.com 以通过 htaccess 永久重定向到 https://example.com - Resolving http://www.example.com to permanently redirect to https://example.com via htaccess 如何将https://www.example.com重定向到https://example.com - How to redirect https://www.example.com to https://example.com 如何将 https://www.example.com/aaa 重定向到 https://example.com/aaa/ - how to redirect https://www.example.com/aaa to https://example.com/aaa/ 如何使 htaccess 强制 https 用于 www.example.com 和 example.com 但没有其他子域 - how to make htaccess force https for www.example.com and example.com but no other subdomains
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM