去除网址“的index.html',并加上”WWW“与一个单一的301重定向 [英] Removing 'index.html' from url and adding 'www' with one single 301 redirect

查看:518
本文介绍了去除网址“的index.html',并加上”WWW“与一个单一的301重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了从网址我用下面的删除 index.html的的index.htm 我的的.htaccess

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 *)

然后才能添加 WWW 在URL中我用下面的我的.htaccess

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]

这工作呢!

这里的问题是如何避免类似下面的例规则上面创建的双重重定向:

  1. 浏览器要求 http://mydomain.com/path/index.html
  2. 服务器发送 301 头redircet浏览器 http://mydomain.com/path/
  3. 在随后的浏览器请求 http://mydomain.com/path/
  4. 现在,在服务器发送 301 头redircet浏览器 http://www.mydomain.com/path /
  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/

这显然不是很聪明的原因较差的用户是谁问 http://mydomain.com/path/index.html 将双重定向,并且他会感觉页转到速度太慢。此外Googlebot的可能会停止以下链接事业双redircetion(我不知道在这最后一个,我不想进入这个讨论,它只是一个可能的问题。)

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.)

谢谢!

*向谁可能会感兴趣:

*To whom it might be interested:

  • NC 用于还重定向 大写的文件,即 INDEX.HTML / 的index.htm
  • NE 用于 避免双重URL编码我避免 的http://.../index.html你好= BA%20be 被重定向到 的http://.../index.html你好= BA%2520be
  • <打击> QSA 用于重定向 同样的查询,即 的http://.../index.html你好=贝贝的http://.../你好=贝贝 (不需要感谢<一href="http://stackoverflow.com/questions/6059920/removing-index-html-from-url-and-adding-www-with-one-single-301-redirect/6062534#6062534">anubhava答案)
  • 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)

推荐答案

要避免双重重定向在.htaccess文件满足两个条件类似这样的另一条规则:

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]

因此​​,如果输入的网址是 http://mydomain.com/path/index.html 那么这两个条件得到满意的在这里的第一个规则,将有1单重定向(301) http://www.mydomain.com/path/

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/.

此外,我相信 QSA 标记是不是真的需要上面的,因为你是不会操作查询字符串。

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

这篇关于去除网址“的index.html',并加上”WWW“与一个单一的301重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆