存档

文章标签 ‘301’

更改了wordpress的固定链接格式后做301重定向

2010年3月11日 2 条评论

最近总是感觉以前的URL格式太丑陋,太过杂乱。于是乎一时冲动就把URL格式给改了。
当然改了URL的首要事情就是要把以前就的url给重定向到新的URL上来,以减少搜索引擎方面的损失。
对 于wordpress来说你可以很容易的做的这一点。一个名叫Dean’s Permalinks Migration的插件可以让你很方便的帮你把原来的链接重定向过来。Dean’s Permalinks Migration安装使用很简单,只需激活,先设置旧的WordPress伪静态格式,然后再WordPress固定链接设置中填写新的地址格式即可。 类似插件还有Advanced Permalinks,好像功能更加强大点。

很不幸的是我不是一个wordpress插件的爱好者,我的博客力求使用尽量少的第三方插件。这里我来重点谈谈apache 的.htaccess文件设置301重定向。其实我对这.htaccess文件一点也不懂,根本不知道怎么写规则,于是就到apache官网上现学现卖 了。

如何重定向以及关于正则表达式的使用apache的文档讲的很详细,http://httpd.apache.org/docs/2.2/rewrite/rewrite_intro.html

http://httpd.apache.org/docs/2.2/urlmapping.html

我就简单的说说正则表达式,
^   –> 匹配字符串的开始
$   –> 匹配字符串的结束
\s  –> 匹配任意的空白符
\w –> 匹配字母或数字或下划线或汉字
\b  –> 是正则表达式规定的一个特殊代码代表着单词的开头或结尾,也就是单词的分界处。
\d  –> 匹配一位数字
\W(大写) –> 匹配任意不是字母,数字,下划线,汉字的字符
\S (大写) –> 匹配任意不是空白符的字符
\D (大写) –> 匹配任意非数字的字符
\B(大写) –> 匹配不是单词开头或结束的位置
” . “   –> 匹配除了换行符以外的任意字符
” * “   –> 它代表的不是字符,也不是位置,而是数量——它指定 * 前边的内容可以连续重复使用任意次以使整个表达式得到匹配。” .* “连在一起就意味着任意数量的不包含换行的字符
” + “   –> 是和 ” * ” 类似的元字符,不同的是 * 匹配重复任意次(可能是0次),而 + 则匹配重复1次或更多次。
” ? “   –> 重复零次或一次
” – “  –> 不是元字符,只匹配它本身 连字符(减号)

需要说明的是我以前的url形式/%postname%-%post_id%.html,现在的使用的是/archives/%post_id%。那么我以前的网址是www.99xunle.com/xunlei-guanggao-660.html现在变为了www.99xunle.com/archives/662

那么.htaccess写法如下:

RedirectMatch 301 ^/(.+)-(\d+)\.html$ /archives/$2

同样我的tag,author,category,date 目录也变了就做个目录定向。

Redirect /author /archives/author
Redirect /date /archives/date
Redirect /category /archives/category
Redirect /tag /archives/tag

更改了url,做了重定向Google已经有了反应,百度却貌似没反应,还是做好被百度冷淡20多天的心理准备吧。

分类: WP相关 标签: , ,

修改.htaccess实现301重定向

2010年3月11日 没有评论

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

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

2.重定向www.domain.com到domain.com

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

3.重定向olddomain.com到www.newdomain.com

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

4.重定向olddomain.com to newdomain.com

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

5.重定向domain.com/file/file.php 到 otherdomain.com/otherfile/other.php

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

分类: 建站相关 标签: ,

301转向代码合集

2010年2月1日 没有评论

1、IIS下301设置

Internet信息服务管理器 -> 虚拟目录 -> 重定向到URL,输入需要转向的目标URL,并选择“资源的永久重定向”。

2、ASP下的301转向代码

<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.lesishu.cn/articles/301/”
%>

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://www.lesishu.cn/articles/301/“);
}
</script>

4、PHP下的301转向代码

header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://www.lesishu.cn/articles/301/”);
exit();

5、CGI Perl下的301转向代码

$q = new CGI;
print $q->redirect(”http://www.new-url.com/”);

6、JSP下的301转向代码

<%
response.setStatus(301);
response.setHeader( “Location”, “http://www.lesishu.cn/” );
response.setHeader( “Connection”, “close” );
%>

7、Apache下301转向代码

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

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

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

2)重定向到新域名

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.lesishu.cn/$1 [L,R=301]

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

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

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

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

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

<VirtualHost *:80>
ServerName www.lesishu.cn
DocumentRoot /home/lesishu
</VirtualHost>

<VirtualHost *:80>
ServerName lesishu.cn
RedirectMatch permanent ^/(.*) http://www.lesishu.cn/$1
</VirtualHost>

Apache下除了以上2种方法,还有其他配置方法和可选参数,建议阅读Apache文档

301转向情况检测

  1. http://www.seoconsultants.com/tools/headers.asp
  2. http://www.internetofficer.com/seo-tool/redirect-check/
分类: 编程开发 标签: ,

WordPress搬家Win主机301重定向教程

2010年1月29日 没有评论

只需把wp根目录下wp-blog-header.php文件修改一下即可:
在〈?php后面换行加上以下代码

if (strtolower($_SERVER['SERVER_NAME'])!=’wuleilei.net’)
{
$URIRedirect=$_SERVER['REQUEST_URI'];
if(strtolower($URIRedirect)==”/index.php”)
{
$URIRedirect=”/”;
}
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location:http://wuleilei.com’.$URIRedirect);
exit();
}
分类: WP相关 标签: , ,

301转向代码的.htaccess

2009年11月18日 没有评论

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www.21andy.com$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.21andy.com/folder [R=301,L]