.htaccess 中不同协议的 URL 重写

发布于 2024-09-17 01:20:06 字数 559 浏览 1 评论 0原文

我需要有关 .htaccess 中的 url 重写的帮助。

所以问题是关于不同的协议:https 和 http。重写的主要目的是从 URL 中删除“www”,但协议应保持与之前相同。

例如,当我有像 http://www.domain.com/request 这样的 URL 时,它应该重定向到 http://domain.com/request。我用以下规则解决它:

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

但以防万一,当 URL 看起来像 https://www.domain.com/request 时,它应该重定向到 https://domain .com/请求。

不幸的是,无论当前协议如何,上述规则都将重定向到 http

提前致谢。

I need help with url-rewriting in .htaccess.

So the issue is about different protocols: https and http. The main purpose of rewriting is to remove "www" from URL, but protocol should stay the same it was before.

For example, when I have URL like http://www.domain.com/request, it should be redirected to the http://domain.com/request. I resolve it with these rules:

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

But in case, when URL looks like https://www.domain.com/request it should be redirected to https://domain.com/request.

Unfortunately, the above rule will redirect to http regardless current protocol.

Thanks in advance.

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

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

发布评论

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

评论(1

沉默的熊 2024-09-24 01:20:06

这与链接的可能重复项非常相似,但由于它会强制 www 到您想要删除它的位置,因此可能需要一个单独的答案。

尝试这样的事情:

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

This is fairly similar to the linked possible duplicate, but since that one forces www where you want to remove it, it might warrant a separate answer.

Try something like this:

RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文