Mod 重写 https 问题,从 url 中去除 www

发布于 2024-09-13 21:56:50 字数 483 浏览 1 评论 0原文

我们有一个例如 https://www.egdomain.com/ 的 url,并且 ssl 对egdomain.com 有效。 那么我怎样才能将所有请求从 https://www.egdomain.com/ 重定向到 https://egdomain.com/

该网站也有正常的 http 请求工作正常。

我尝试了下面的 htaccess 但仍然没有

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

RewriteCond %{HTTP_PORT} =443

RewriteRule (.*) https://%1/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

RewriteRule (.*) http://%1/$1 [L,R=301]

任何帮助,我们将不胜感激

we have a url for eg https://www.egdomain.com/ and the ssl is valid for egdomain.com.
so how can i redirect all the requests from https://www.egdomain.com/ to https://egdomain.com/

the site also has normal http requests which works fine.

I tried the htaccess below but still nothing

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

RewriteCond %{HTTP_PORT} =443

RewriteRule (.*) https://%1/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

RewriteRule (.*) http://%1/$1 [L,R=301]

Any help will e much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡墨 2024-09-20 21:56:50

您可以将这两个规则合并为一个:

RewriteCond %{HTTPS}s ^on(s)|
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(https?://)www\.(.+) [NC]
RewriteRule ^ %1%2 [L,R=301]

但这可能有点太混乱了。

You can combine these two rules to one:

RewriteCond %{HTTPS}s ^on(s)|
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(https?://)www\.(.+) [NC]
RewriteRule ^ %1%2 [L,R=301]

But maybe this is a little too confusing.

猛虎独行 2024-09-20 21:56:50

手册说(强调我的):

RewriteCond 反向引用:这些是 %N (1 <= N <= 9) 形式的反向引用,提供对模式的分组部分(同样在括号中)的访问,从当前条件集中的最后匹配 RewriteCond 开始。

因此:

RewriteCond %{HTTP_PORT} =443
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) https://%1/$1 [L,R=301]

请注意,如果您的证书仅对不带 www. 的变体有效,访问者在访问 www. 变体时仍会收到安全警告。

The manual says (emphasis mine):

RewriteCond backreferences: These are backreferences of the form %N (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions.

So:

RewriteCond %{HTTP_PORT} =443
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) https://%1/$1 [L,R=301]

Note that if your certificate is only valid for the variant without www., visitors will still get a security warning when accessing the www. variant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文