apache2.4 设置重定向详解
域名重定向是在网站配置中是经常用到的,不管是因为seo还是改版或是https 等等都需要配置重定向:
第一种方法,通过 Rewrite实现重定向;将xcwmoon.com跳转到https://www.xcwmoon.com包括后面的参数也一并带过去.
<VirtualHost *:80> ServerName xcwmoon ServerAlias xcwmoon.com RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^(.*) https://www.%{SERVER_NAME}$1 [L,R=301] </VirtualHost>
第二种:这个就是直接的跳转了,效果与第一种方法的效果是一样的,RedirectMatch还有其他写法
<VirtualHost *:80> ServerName xcwmoon ServerAlias xcwmoon.com RedirectMatch permanent "(.*)$" "https://www.xcwmoon.com$1" #301的方式跳转,permanent也是301跳转 #RedirectMatch 301 "(.*)$" "https://www.xcwmoon.com$1" #RedirectMatch temp ".*" "http://othersite.example.com/startpage.html" </VirtualHost>
下面是参数:
permanent #表示301重定向
temp #表示302重定向
seeother #表示303重定向
gone #表示401重定向
为了方便可以直接用 :RedirectMatch 301 "(.*)$" "https://www.xcwmoon.com$1"
注意:如果使用RedirectMatch跳转,官方文档说了如果状态码在300-399之间,url参数必须要有,如果不在300-399之前则不能写url参数,状态码必须是有效的状态码,也就是如果你自己定了一个响应状态码是不能用在这儿的。
留言(0)