在Unix/Linux主机上设置301永久重定向的方法
在Unix/Lux主机上设置的方法
可以用.htaess文件
1. Apache模块 mod_alias的 Redirect 和 RedirectMatch命令
上面提到2个命令使用方法相似。而区别就是后者RedirectMatch基于正则表达式匹配对当前的URL发送一个外部重定向语法为
Redirect [status] URL-path URL
RedirectMatch [status] regex URL
status参数可以使用推火网以下HTTP状态码
permanent
返回一个永久性重定向状态码(301),表示此资源的位置变动是永久性的。
temp
返回一个临时性重定向状态码(302),这是默认值。
seeother
返回一个“参见”状态码(303),表示此资源已经被替代。
gone
返回一个“已废弃”状态码(410),表示此资源已经被永久性地删除了。如果指定了这个状态码,则URL参数将被忽略。
举例
APACHE
Redirect 301 /old/old.htm http://.###./new.htm
Redirect permanent /one http://###./two
RedirectMatch 301 (.).gif$ http://.###./images/$1.jpg
2.使用mod_rewrite重写URL方式
APACHE
Options +FollowSymLks
RewriteEnge on
RewriteCond %{HTTP_HOST} ^####.RewriteRule ^(.)$ http://.###./$1 [R=permanent,L]
在这里判断当前服务器变量HTTP_HOST是否等于###.,为真就进行重写,按照R=permanent进行永久重定向,L表示并立即停止重写操作,并不再应用其他重写规则
下面是最终实现的.htaess文件,也并入wordpress重写规则。
APACHE
# BEGIN WordPress
RewriteEnge On
#Redirect
Options +FollowSymLks
RewriteCond %{HTTP_HOST} ^###.$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.)$ http://.###./$1 [R=301,L]
#Rewrite(blog)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/dex.php [L]
RewriteRule . -
# END WordPress