您现在的位置是:首页 > 其他

李清波 2014-06-26 其他 51203 复制当前网址

不带www转向带www301重定向的方法apache+window环境


apache中我们以htaccess文件为主。


我们以本站http://www.liqingbo.cn为例子


我们现在就先来看一下如果通过修改htaccess文件做301重定向,实现域名转向


第一种方法:

将不带www的顶级域名转向带www的二级域名:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^liqingbo.cn [NC] 
RewriteRule ^(.*)$ http://www.liqingbo.cn/$1 [L,R=301]


第二种方法:

通过修改apache的配置文件来实现

一般我们自己拥有自己的服务器的时候,都有可能会配置多站点,多站点的方法在这里我就不多说了。

<VirtualHost *:80> 
DocumentRoot “E:/xxx” 
ServerName 
http://www.域名.net 
</VirtualHost> 
<VirtualHost *:80> 
ServerName 域名.net 
RedirectMatch permanent ^/(.*) http://www.域名.net$1 
</VirtualHost>


在.htaccess中,Redirect重定向包含(不支持正则匹配)


第一个是permanent:永久性301重定向;
第二个是temp:临时重定向状态码,即返回302;
第三个是seeother:此参数表示“参见”的含义,即本资源已经被另外一个更好的资源替代了;

Redirect [permanent,temp,seeother]/service http://www.baidu.com/service


RedirectMatch函数支持正则

RedirectMatch 301 reed\/(.*)\.html$ http://liqingbo.cn/$1/
RedirectMatch 301 reed\/tag\/(.*)$ http://liqingbo.cn/tag/$1/


上面的也只是一些简单的例子,等我有时间了,再重新整理一下。


再者,301重定向还有几种方法:

RewriteRule ^A$ B [L,R=301]
RedirectMatch 301 ^A$ B
Redirect 301 A B

文章来源:http://liqingbo.com/blog-311.html

评论