当前位置: 首页 > 图文教程 > 网站运营 > 建站经验 > 不同环境下网站进行301重定向的代码集锦

建站经验
十年网建翻天覆地变化 正规化建站已来临
meta中的关键字标签 Keywords 优化技巧
一个草根站长对CN域名的看法
合理改版的价值所在 网站改版的3要3不要
聚有客之路(二)对Google AdSense的期待
值得回味 进行全站优化必须做的26件事情
个人站长必备的100条广告版块版规用语
2009年 比博客访问量更加重要的几个指标
DedeCMS V5.3自定义模型使用教程详解
同城分类网站信息发布管理的几个经验
简单分析3款小巧cms建站程序
网站架构优化:前端程序服务器三者的协调
没有技术,你也可以很优秀!—我的站长生涯
网站运营模式观察与分析
大型网站的运营指标
域名经济时代资源稀少 域名选择经验再分享
建站经验:冷门行业网站站长应该如何坚守
抛弃讨厌的SEO 让网站取得成功的10步骤
关于博客建设的边缘话题 原创写作心得分享
地方网站宣传经费开销一览

建站经验 中的 不同环境下网站进行301重定向的代码集锦


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 97 ::
收藏到网摘: n/a

301重定向,也称为301转向。它的功能就是告诉搜索引擎,你的网址永久的变换了,从而将原网址的权重全部定向到新的网址上面,相信很多人都会用到301重定向的。

1、ASP下的301转向代码

<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://ruanchen.com/"#000000">3、ASP.Net下的301转向代码

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”http://ruanchen.com/"#000000">4、PHP下的301转向代码

header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://ruanchen.com/"#000000">5、CGI Perl下的301转向代码

$q = new CGI;
print $q->redirect(”http://ruanchen.com/"#000000">

6、JSP下的301转向代码

<%
response.setStatus(301);
response.setHeader( “Location”, “http://ruanchen.com/"#000000">7、Apache下301转向代码

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

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

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^lesishu.cn [NC]
RewriteRule ^(.*)$ http://ruanchen.com/"#000000">2)重定向到新域名

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://ruanchen.com/"#000000">3)使用正则进行301转向,实现伪静态

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+).html$ news.php?id=$1

将news.php?id=123这样的地址转向到news-123.html