虚拟主机怎么做301重定向?设置301重定向规则,生成文件放入到根目录,即可实现虚拟主机301重定向。
什么时候需要做301重定向?一是为了规范url,集中权重;二是,网站改版,将旧版页面转移到新版页面上。
比如,网站首页的url有以下几种:
xxx.com
www.xxx.com
xxx.com/index.html
www.xxx.com/index.html
以及http页面和https页面等多种。一个首页出现如此多url,一是不规范,二是权重分散,这时我们就需要将所有指向首页的url统一规范到某一个,比如:www.xxx.com或https://www.xxx.com/。
还有页面的url改变了,那么我们也需要做一个301重定向,避免老的url产生404。
那么我们怎么做301重定向呢?要区分linux虚拟主机还是windows虚拟主机,两者301重定向规则不同。
linux虚拟主机,一般伪静态规则放入.htaccess文件。
<ifmodule mod_rewrite.c>
rewriteengine on
rewritecond %{http:from-https} !^on$ [nc]
rewritecond %{http_host} ^(www.)?xxx.com$ [nc]
rewriterule ^(.*)$ https://www.xxx.com/$1 [r=301,l]
</ifmodule># 将xxx.com和www.xxx.com跳转到https://www.xxx.com,防止apache子站继承上级目录.htaccess受影响,注释请自行删除,不需要放入htaccess文件
如果有多个需求,如:a.com跳转到到www.a.com;b.com跳转到www.b.com,规则如下:
rewriteengine on
rewritecond %{http_host} ^a.com$ [nc]
rewriterule ^(.*)$ http://www.a.com/$1 [r=301,l]
rewritecond %{http_host} ^b.com$ [nc]
rewriterule ^(.*)$ http://www.b.com/$1 [r=301,l]根据自身网站需要添加修改,然后上传规则到服务器wwwroot根目录的.htaccess文件里。如没有.htaccess文件,新建一个文本文档,并重命名为.htaccess。
windows虚拟主机,一般伪静态规则放入到web.config文件。
<?xml version=1.0 encoding=utf-8?>
<configuration>
<system.webserver>
<rewrite>
<rules>
<rule name=301 stopprocessing=true>
<match url=^(.*)$ ignorecase=false />
<conditions logicalgrouping=matchall>
<add input={http_from_https} pattern=^on$ negate=true />
</conditions>
<action type=redirect url=https://www.xxx.com/{r:1} redirecttype=permanent />
</rule>
</rules>
</rewrite>
</system.webserver>
</configuration># www.xxx.com对应修改为您自已的域名,注释请自行删除,不需要放入web.config文件内
以上是关于“虚拟主机怎么做301重定向”的介绍,如需购买虚拟主机,首推西部数码:https://www.west.cn/services/webhosting/