最全301重定向

网页301方法

网页又分两种:

1》利用浏览器refresh,其中content表示延迟3秒

<html>  
<head>  
<meta http-equiv="Content-Type" context="text/html;charset=gb2312"/>  
<meta http-equiv="Refresh" content="3;url=http://www.xxx.net/"/>  
</head>  
<boay>  
<p>  
您的URL是<a href="http://www.xxx.net/xxx">xxxx</a>  
</p>  
<p>3秒后页面跳转。</p>  
<p>3秒后页面未跳转,请点击上面的连接。</p>  
</body>  
</html> 

  

2》利用iframe

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Fenshang.com</title><frameset framespacing="0" border="0" rows="0" frameborder="0"><frame name="main" src="http://xxx.xxx.com" scrolling="auto" noresize></frameset>
</head>
<body>
<a href="http://www.xxx.com">xxx</a>
</body>
</html>

PHP后台301方法

<?php
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.xx.com");
?>

服务器301方法

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.php100.com/new.htm
Redirect permanent /one http://php100.com/two
RedirectMatch 301 (.*).gif$ http://www.php100.com/images/$1.jpg


2.使用mod_rewrite重写URL方式

APACHE

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


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

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

APACHE


<IfModule mod_rewrite.c>
RewriteEngine On
#Redirect
Options +FollowSymLinks
RewriteCond %{HTTP_HOST}   ^php100.com$
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^(.*)$ http://www.php100.com/$1 [R=301,L]
#Rewrite(blog)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/.* /blog/index.php [L]
RewriteRule . -
</IfModule>

原文地址:https://www.cnblogs.com/huhu1020387597/p/12894556.html