• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

asp.net php asp jsp 301重定向的代码

php 搞代码 4年前 (2022-01-26) 21次浏览 已收录 0个评论

介绍一下针对各类程序系统实施301重定向的代码:
1、Linux主机重定向
Godaddy的Liunx主机,Godaddy本身已经支持Apache,所以直接创建一个.htaccess文件就可以了,一般来说,在本地无法创建.htaccess的时候可以先创建一个txt格式文件,上传到根目录的时候再重命名为“.htaccess”就可以了。网上很多如何进行 301重定向的教程,无论是整站重定向还是单页重定向。下面就以我的www.wecanwecan.com为例
1.1 无www域名转移到www域名
RewriteEngine on
rewritecond %{http_host} ^wecanwecan.com [nc]
rewriterule ^(.*)$ http://www.wecanwecan.com/$1 [r=301,nc]
1.2 整站301重定向
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wecanwecan.com [NC]
RewriteRule ^(.*)$ http://www.wecanwecan.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.wecanwecan.com [NC]
RewriteRule ^(.*)$ http://wecanwecan.com/$1 [L,R=301]

另外一种是在根目录下的index.php里这样弄

header(“HTTP/1.1 301 Moved Permanently”);
header(“Location:http://wecanwecan.com/”);
exit();

2、ASP主机301重定向

在 index.asp 或 default.asp 的最顶部加入以下几行:
代码如下:

<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”,”www.wecanwecan.com ”
Response.End
%>

3、ASP.net主机301重定向

ASP .NET:

Response.Status = “301 Moved Permanently”;

Response.AddHeader(”Location”,”http://www.wecanwecan.com&#8221;);

}

我封装在一个类里:

using System;using System.Collections.Generic;using System.Text;using System.Web.UI;using System.Web.UI.HtmlControls; namespace ClassLib{    public class URLClass    {        private bool flag301 = false;//是否启动 301        private bool isIndex = false;//是否 返回主页 或者保留在当前页        /// <summary>        /// 构造函数        /// </summary>        /// 是否启动 301        /// Page        /// 格式www.xxx.com        public URLClass(bool fl, Page page, string strURL)        {            flag301 = fl;            URL301(page, strURL);        }        /// <summary>        /// 返回主页        /// </summary>        ///         /// 格式www.xxx.com        public void URL301(Page page, string strURL)        {            //301重定向            if (page.Request.Url.DnsSafeHost != strURL && flag301 == true)            {              <p style="color:transparent">2本文来源gao!daima.com搞$代!码网</p><span>搞代gaodaima码</span>  page.Response.Clear();                page.Response.StatusCode = 301;                page.Response.Status = "301 MovedPermanently";                page.Response.AddHeader("Location", "http://" + strURL);                page.Response.End();            }        }    }}
1 2 3 下一页


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:asp.net php asp jsp 301重定向的代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址