.htaccess 中不同协议的 URL 重写
我需要有关 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与链接的可能重复项非常相似,但由于它会强制
www
到您想要删除它的位置,因此可能需要一个单独的答案。尝试这样的事情:
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: