使用Redirect或urlRewrite在.htaccess文件里设置301跳转, .htaccess,这个档案通常会在网站的根目录,如果没有,就自己用Notepad新增一个。你的操作系统不允许.htaccess这样的档案名称时,就先把它命名为htaccess.txt,上传到FTP之后,再把档案名称改成.htaccess。

—————网页服务器必须是Apache —————
【情况一】

http://your_domain.com/wordpress>>http://your_domain.com/blog

让连接到/wordpress的连结重新定址到/blog,包含下层路径
例如:http://vinta.ws/wordpress/?p=334会被指向http://vinta.ws/blog/?p=334
在.htaccess中要这么写:
Redirect /wordpress http://your_domain.com/blog
如果有安装mod_rewrite模组的话,也可以这样写:
RewriteEngine on
RewriteRule ^wordpress(.*)$ /blog$1 [R=301,L]
【情况二】http://your_domain.com/wordpress>>http://your_domain.com
让连接到/wordpress的连结重新定址到根目录,包含下层路径(如/wordpress/xxx)
在.htaccess中要这么写:

Redirect /wordpress http://your_domain.com

如果有安装mod_rewrite模组的话,也可以这样写:
RewriteEngine on
RewriteRule ^wordpress(.*)$ $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]

———— BLOG不应该放在根目录啊~ ————

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

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

order allow,deny
deny from all

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

相关日志

5 Responses to “使用Redirect或urlRewrite在.htaccess文件里设置301转向”

  1. 原理都是一样的.自己想着变通一下就可以了..

  2. 基本,没有看懂。。。

  3. 我也是转别人的,不需要看懂,当你有了这方面的需求,自然就会主动去懂了
    说实话,我也没有仔细看过
    有需要修改htaccess的操作时,我都是通过这个页面来生成它
    http://www.htaccesseditor.com/sc.shtml

  4. 想要在Windows下用记事本或其它文本编辑器保存出来一个.htaccess文件,只要在保存文件的时候在文件名文本框中输入”.htaccess”(包含双引号)就可以了。

  5. 谢谢提供这个技巧!

Leave a Reply