当前位置:SEO自学网 > 网站SEO优化教程 > 网站结构优化 > 在Unix/Linux主机上设置301永久重定向的方法

在Unix/Linux主机上设置301永久重定向的方法

admin2022年03月31日 21:09:51网站结构优化361

在Unix/Linux主机上设置的方法

可以用.htaccess文件

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://www.###.com/new.htm

Redirect permanent /one http://###.com/two

RedirectMatch 301 (.*).gif$ http://www.###.com/images/$1.jpg

2.使用mod_rewrite重写URL方式

APACHE

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} ^####.comRewriteRule ^(.*)$ http://www.###.com/$1 [R=permanent,L]

在这里判断当前服务器变量HTTP_HOST是否等于###.com,为真就进行重写,按照R=permanent进行永久重定向,L表示并立即停止重写操作,并不再应用其他重写规则

下面是最终实现的.htaccess文件,同时也并入wordpress重写规则。

APACHE

# BEGIN WordPress

RewriteEngine On

#Redirect

Options +FollowSymLinks

RewriteCond %{HTTP_HOST} ^###.com$

RewriteCond %{HTTP_HOST} !^$

RewriteRule ^(.*)$ http://www.###.com/$1 [R=301,L]

#Rewrite(blog)

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^blog/.* /blog/index.php [L]

RewriteRule . -

# END WordPress


扫描二维码推送至手机访问。

版权声明:本文由SEO自学网发布,如需转载请注明出处。

本文链接:http://www.seozixuewang.com/post/360.html

“在Unix/Linux主机上设置301永久重定向的方法” 的相关文章