删除“index.html”从 url 并添加“www”带有一个 301 重定向
为了从网址中删除 index.html
或 index.htm
,我在 .htaccess
中使用以下内容
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]
这有效!(有关此问题末尾的标志的更多信息*)
然后,为了在网址中添加 www
,我在 .htaccess
中使用以下内容
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ "http://www.mydomain.com/$1" [R=301,NE,L]
这有效也是!
这里的问题是在如下情况下如何避免上述规则创建的双重重定向:
- 浏览器要求
http://mydomain.com/path/index.html
- 服务器发送
301
标头将浏览器重定向到http://mydomain.com/path/
, - 然后浏览器请求
http ://mydomain.com/path/
- 现在是服务器发送
301
标头将浏览器重定向到http://www.mydomain.com/path/
这显然不是很聪明,因为一个可怜的用户正在询问http://mydomain.com/path/index.html
会被双重重定向,他会觉得页面速度太慢。此外,Googlebot 可能会停止跟踪双重重定向的链接原因(我不确定最后一个,我不想对此进行讨论,这只是另一个可能的问题。)
谢谢!
*可能感兴趣的人:
NC
也用于重定向 大写文件即INDEX.HTML
/InDeX.HtM
- 使用
NE
为了避免双重 url 编码,我避免http://.../index.html?hello=ba%20be
被重定向到http://.../index.html?hello=ba%2520be
(由于anubhava 答案)QSA
用于重定向 还查询,即http://.../index.html?hello=babe
到http://.../?hello=babe
In order to remove index.html
or index.htm
from urls I use the following in my .htaccess
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]
This works! (More info about flags at the end of this question *)
Then in order to add www
in urls I use the following in my .htaccess
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ "http://www.mydomain.com/$1" [R=301,NE,L]
This works too!
The question here is how to avoid the double redirection created by rules above in cases like the one below:
- browsers asks for
http://mydomain.com/path/index.html
- server sends
301
header to redircet browser tohttp://mydomain.com/path/
- then browser requests
http://mydomain.com/path/
- now the server sends
301
header to redircet browser tohttp://www.mydomain.com/path/
This is obviously not very smart cause a poor user who is asking http://mydomain.com/path/index.html
would be double redirected, and he would feel page goes too slow. Moreover Googlebot might stop following the link cause to the double redircetion (I'm not sure on this last one and I don't want to get into a discussion on this, it's just another possible issue.)
Thanks!
*To whom it might be interested:
NC
is used to redirect also
uppercased files i.e.INDEX.HTML
/InDeX.HtM
NE
is used
to avoid double url encoding I avoidhttp://.../index.html?hello=ba%20be
to be redirected tohttp://.../index.html?hello=ba%2520be
(not needed thanks to anubhava answer)QSA
is used to redirect
also queries, i.e.http://.../index.html?hello=babe
tohttp://.../?hello=babe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了避免双重重定向,.htaccess 文件中还有另一个满足两个条件的规则,如下所示:
因此,如果输入 URL 为
http://mydomain.com/path/index.html
那么这两个条件都满足这里的第一条规则将有 1 个重定向 (301) 到http://www.mydomain.com/path/
。另外,我相信上面并不真正需要
QSA
标志,因为您NOT 操作查询字符串。To avoid double redirection have another rule in .htaccess file that meets both conditions like this:
So if input URL is
http://mydomain.com/path/index.html
then both the conditions get satisfied in the first rule here and there will be 1 single redirect (301) tohttp://www.mydomain.com/path/
.Also I believe
QSA
flag is not really needed above since you are NOT manipulating query string.更好的解决方案是将index.html 规则放在www 规则之前,并在index.html 规则内部将www 前缀添加到目标url。这样,寻找 http://domain.com/index.html 的人就会被发送到 http://www.domain.com/ 按第一条规则。第二个 (www) 规则仅在索引 AND www 缺失时适用,这又只是一个重定向。
A better solution would be to place the index.html rule ahead of the www rule and inside the index.html rule ADD the www prefix to the destination url. This way someone looking for http://domain.com/index.html would get sent to http://www.domain.com/ by the FIRST rule. The second (www) rule would then only apply if index AND www are missing, which is again only one redirect.
从之前的规则中删除
L
标志?L
强制停止规则解析(当规则匹配时),从而发送第一个重写的 URL,而不应用第二个规则。规则从上到下按顺序应用,如果 URL 与规则的条件和模式匹配,则每次都再次重写 URL。
因此,在发送新 URL 之前,上面将首先添加
www
,然后删除index.html?
; 所有规则的单一重定向。Remove the
L
flag from the prior rule?L
forces the rule parsing to stop (when the rule is matched) and thus send the first rewritten URL without applying the second rule.The rules are applied sequentially from top to bottom, each rewriting the URL again if it matches the rule's conditions and pattern.
Hence the above will first add the
www
and then remove theindex.html?
, before sending the new URL; A single redirect for all the rules.