htaccess:如何以通用方式强制 www
RE: .htaccess - 如何强制“www”。以通用方式?
我之前问过这个问题,并得到了这个答案:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
它有效,但现在我在响应标头中看到301 Moved Permanently
。我想消除 301。问题是第二次重写条件吗?是否应该是“不以‘www’开头。”后跟主机名”?
顺便说一句,我希望这个解决方案适用于任何服务器(也就是说,我不想对我的域名进行硬编码)。
建议?
更新:
我刚刚意识到上述内容无法正常工作。如果我有以下内容:
http://images.domain.com
我不希望将其更改为:
http://www.images.domain.com
我不希望这影响子域。我只希望它影响缺失的 www
。
RE: .htaccess - how to force "www." in a generic way?
I asked this question before, and got this answer:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It works, but now I am seeing 301 Moved Permanently
in the response headers. I want to eliminate the 301s. Is the problem the 2nd rewrite condition? Should it be something like "does not start with 'www.' followed by the host name"?
By the way, I want this solution to work for any server (meaning, I don't want to hard code my domain name).
Suggestions?
UPDATE:
I just realized that the above is not working correctly. If I have the following:
http://images.domain.com
I don't want that to change to:
http://www.images.domain.com
I don't want this affecting sub-domains. I only want it to affect missing www
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据定义,如果没有某些标头重定向,则重定向到不同的域是不可能的。如果您希望用户浏览器中的 URL 发生更改,则必须强制发出新请求。没有办法解决这个问题。
您将进行选择 -
301、
302
和303
状态代码是最直接的选择。Redirecting to a different domain is by definition not possible without some header redirect. If you want the URL in the user's browser to change, you have to force a new request. There is no way around that.
You will have to take your pick - the
301
,302
and303
status codes being the most straight forward choices.