使用Redirect或urlRewrite在.htaccess文件里设置301跳转

2008-08-06 17:16
使用Redirect或urlRewrite在.htaccess文件里设置301跳转, .htaccess,这个档案通常会在网站的根目录,如果没有,就自己新增一个。你的操作系统不允许.htaccess这样的档案名称时,就先把它命名为htaccess.txt,上传到FTP之后,再把档案名称改成.htaccess。
—————网页服务器必须是Apache —————
【情况一】
http://your_domain.com/xyz>>http://your_domain.com/abc
让连接到/xyz的连结重新定址到/abc,包含下层路径
例如:http://vinta.ws/xyz/?p=334会被指向http://vinta.ws/abc/?p=334
在.htaccess中要这么写:
Redirect /xyz http://your_domain.com/abc
如果有安装mod_rewrite模组的话,也可以这样写:
RewriteEngine on
RewriteRule ^xyz(.*)$ /abc$1 [R=301,L]
【情况二】
http://your_domain.com/xyz>>http://your_domain.com
让连接到/xyz的连结重新定址到根目录,包含下层路径(如/xyz/xxx)
在.htaccess中要这么写:

Redirect /xyz http://your_domain.com

如果有安装mod_rewrite模组的话,也可以这样写:
RewriteEngine on
RewriteRule ^xyz(.*)$ $1 [R=301,L]

【情况三】
http://old_domain.com/>>http://new_domain.com/
让连接到旧网址的连结重新定址到新网址,前提是你必须是旧网址的拥有者
建议让旧网址和新网址包持相同的目录结构

把.htaccess放到旧网址的根目录,然后要这么写:

RewriteEngine on
RewriteRule (.*) http://new_domain.com/$1 [R=301,L]

【情况四】
http://www.your_domain.com/>>http://your_domain.com/

统一你的网址,不要出现www
由www.your_domain.com进入的连结一律重新指向your_domain.com

可以在.htaccess中这么写:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.your_domain\.com$ [NC]
RewriteRule ^(.*)$ http://your_domain.com/$1 [R=301,L]


【技巧一】
确保你的网站实行了301 Redirect,可以到Search Engine Friendly Redirect Checker检查。输入要检查的网址和验证码就可以了。

【技巧二】
防止.htaccess档案被检视,则要在.htaccess中加入:

order allow,deny
deny from all

【技巧三】
通常该目录中没有index.html的时候,Apache会把此目录下的档案统统列出来。如果你不想这么做的话,在.htaccess中加入这一行:
Options -Indexes

198 次阅读 | 0 个评论

留下脚印