301重定向代码合集(iis,asp,php,asp.net,apache)

1、IIS下301设置

Internet信息服务管理器 -> 虚拟目录 -> 重定向到URL,输入需要转向的目标URL,并选择“资源的永久重定向”。
在IIS中,也可以通过安装ISAPI Rewrite组件来实现如Apache中mod_rewrite的功能,详见ISAPI Rewrite 3下载及常用301规则。

2、ASP下的301重定向代码

<%@ Language=VBScript %> 
<% 
Response.Status=”301 Moved Permanently” 
Response.AddHeader “Location”, “106/” 
%>

3、ASP.Net下的301重定向代码

<script runat=”server”> 
private void Page_Load(object sender, System.EventArgs e) 
{ 
Response.Status = “301 Moved Permanently”; 
Response.AddHeader(”Location”,”  106/“); 
} 
</script> 

4、PHP下的301重定向代码

header(”HTTP/1.1 301 Moved Permanently”); 
header(”Location: 106/”); 
exit(); 

5、CGI Perl下的301重定向代码

$q = new CGI; 
print $q->redirect(””);

6、JSP下的301重定向代码

<% 
response.setStatus(301); 
response.setHeader( “Location”, “” ); 
response.setHeader( “Connection”, “close” ); 
%>

7、Apache下301重定向代码

新建.htaccess文件,输入下列内容(需要开启mod_rewrite):

1)将不带WWW的域名转向到带WWW的域名下

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^embeta.com [NC] 
RewriteRule ^(.*)$    $1 [L,R=301]

2)重定向到新域名

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^(.*)$  $1 [L,R=301]

3)使用正则进行301转向,实现伪静态

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^news-(.+).html1 
将news.php?id=123这样的地址转向到news-123.html

8、Apache下vhosts.conf中配置301重定向

为实现URL规范化,SEO通常将不带WWW的域名转向到带WWW域名,vhosts.conf中配置为:

<VirtualHost *:80> 
ServerName DocumentRoot /home/embeta 
</VirtualHost> 
<VirtualHost *:80> 
ServerName embeta.com 
RedirectMatch permanent ^/(.*)    $1 
</VirtualHost>
    
Apache实现301重定向的几种例子 (修改.htaccess文件 )
    格式:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^网站域名$ [NC]
    RewriteRule ^(.*)$ 别的域名$1 [L,R=301] 或RewriteRule ^(.*)$ 别的文件 [L,R=301]
    
    1.重定向sjolzy.cn到 www.sjolzy.cn

    这种重定向旨在使域名唯一,是网站SEO必须要做的,后面重定向www.sjolzy.cn到 sjolzy.cn也是出于同样的原因,只是形式不同。
    打开.htaccess文件,加入以下规则。(下面的规则是针对主域名的,子域名要修改)

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.sjolzy.cn$ [NC]
    RewriteRule ^(.*)$ http://www.sjolzy.cn/$1 [L,R=301]

    2. 重定向www.sjolzy.cn到sjolzy.cn

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^sjolzy.cn$ [NC]
    RewriteRule ^(.*)$ http://sjolzy.cn/$1 [L,R=301]

    3.重定向oldsjolzy.cn到www.newsjolzy.cn

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !oldsjolzy.cn$ [NC]
    RewriteRule ^(.*)$ http://www.newsjolzy.cn/$1 [L,R=301]

    4.重定向 oldsjolzy.cn to newsjolzy.cn

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !oldsjolzy.cn$ [NC]
    RewriteRule ^(.*)$ http://newsjolzy.cn/$1 [L,R=301]

    5.重定向sjolzy.cn/file/file.php 到 othersjolzy.cn/otherfile/other.php

    RewriteCond %{HTTP_HOST} ^www.sjolzy.cn$
    RewriteRule ^file/file.php$ http://www.othersjolzy.cn/otherfile/other.php [R=301,L]

  

原文地址:https://www.cnblogs.com/forforever/p/12673514.html