删除“index.html”从 url 并添加“www”带有一个 301 重定向

发布于 2024-11-08 14:31:40 字数 1703 浏览 0 评论 0原文

为了从网址中删除 index.htmlindex.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]

这有效也是!

这里的问题是在如下情况下如何避免上述规则创建的双重重定向:

  1. 浏览器要求 http://mydomain.com/path/index.html
  2. 服务器发送 301 标头将浏览器重定向到 http://mydomain.com/path/
  3. 然后浏览器请求 http ://mydomain.com/path/
  4. 现在是服务器发送 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
  • QSA 用于重定向 还查询,即 http://.../index.html?hello=babehttp://.../?hello=babe (由于anubhava 答案

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:

  1. browsers asks for http://mydomain.com/path/index.html
  2. server sends 301 header to redircet browser to http://mydomain.com/path/
  3. then browser requests http://mydomain.com/path/
  4. now the server sends 301 header to redircet browser to http://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 avoid
    http://.../index.html?hello=ba%20be
    to be redirected to
    http://.../index.html?hello=ba%2520be
  • QSA is used to redirect
    also queries, i.e.
    http://.../index.html?hello=babe to
    http://.../?hello=babe
    (not needed thanks to anubhava answer)

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

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

发布评论

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

评论(3

赤濁 2024-11-15 14:31:40

为了避免双重重定向,.htaccess 文件中还有另一个满足两个条件的规则,如下所示:

Options +FollowSymlinks -MultiViews
RewriteEngine on

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

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule . http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . %1 [R=301,NE,L]

因此,如果输入 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:

Options +FollowSymlinks -MultiViews
RewriteEngine on

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

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule . http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . %1 [R=301,NE,L]

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) to http://www.mydomain.com/path/.

Also I believe QSA flag is not really needed above since you are NOT manipulating query string.

情未る 2024-11-15 14:31:40

更好的解决方案是将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.

深海蓝天 2024-11-15 14:31:40

从之前的规则中删除 L 标志? L 强制停止规则解析(当规则匹配时),从而发送第一个重写的 URL,而不应用第二个规则。

规则从上到下按顺序应用,如果 URL 与规则的条件和模式匹配,则每次都再次重写 URL。

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteRule ^(.*/)index\.html?$ $1 [NC,QSA,R=301,NE,L]

因此,在发送新 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.

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteRule ^(.*/)index\.html?$ $1 [NC,QSA,R=301,NE,L]

Hence the above will first add the www and then remove the index.html?, before sending the new URL; A single redirect for all the rules.

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