
301重定向,是一种非常重要的“自动转向”技术。比如,我们访问35.com时,出现的却是https://www.35.com/;访问www.35.com时,也是到的https://www.35.com/这个页面。这个就是做了301重定向,将不同域名形式统一到一个URL,可以实现URL规范化,给用户、搜索引擎留下好的印象。
1、Linux系统虚拟主机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文件
2、Windows系统虚拟主机301重定向怎么做
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文件内
当然,除了以上的配置文件方式,我们也可以直接采用代码方式:
Windows虚拟主机,ASP语言的:
Response.Status="301 Moved Permanently" Response.AddHeader "Location","https://www.xxx.com" Response.End
Linux虚拟主机,PHP语言的:
header("HTTP/1.1 301 Moved Permanently");
header("Location:https://www.xxx.com");
exit();
说明:把代码加到相关页面就可以了。 一般放在头部文件里。
以上是关于虚拟主机301重定向怎么做的方法介绍,如需购买虚拟主机,可到三五互联:https://www.35.com/services/webhosting/


