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

简介: 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]
目录
打赏
0
0
0
0
8
分享
相关文章
ASP.NET Web Pages - 添加 Razor 代码
ASP.NET Web Pages 使用 Razor 标记添加服务器端代码,支持 C# 和 Visual Basic。Razor 语法简洁易学,类似于 ASP 和 PHP。例如,在网页中加入 `@DateTime.Now` 可以实时显示当前时间。
|
10月前
|
windows_server2012搭建iis并配置http重定向 iis转发
windows_server2012搭建iis并配置http重定向 iis转发
415 1
VB.NET中如何利用ASP.NET进行Web开发
在VB.NET中利用ASP.NET进行Web开发是一个常见的做法,特别是在需要构建动态、交互式Web应用程序时。ASP.NET是一个由微软开发的开源Web应用程序框架,它允许开发者使用多种编程语言(包括VB.NET)来创建Web应用程序。
159 6
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
109 1
在IIS上部署ASP.NET Core Web API和Blazor Wasm详细教程
在IIS上部署ASP.NET Core Web API和Blazor Wasm详细教程
334 3
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
117 0
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
111 0

热门文章

最新文章

推荐镜像

更多