<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>木客网 &#187; 301 redirect</title>
	<atom:link href="http://woodfeed.com/tag/301-redirect/feed/" rel="self" type="application/rss+xml" />
	<link>http://woodfeed.com</link>
	<description></description>
	<lastBuildDate>Wed, 09 Jun 2010 12:15:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>简单PHP代码实现301地址重定向</title>
		<link>http://woodfeed.com/php-code-for-301-redirect/</link>
		<comments>http://woodfeed.com/php-code-for-301-redirect/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 10:57:23 +0000</pubDate>
		<dc:creator>vayu</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[301]]></category>
		<category><![CDATA[301 redirect]]></category>

		<guid isPermaLink="false">http://woodfeed.com/?p=379</guid>
		<description><![CDATA[重定向就是将网页自动转向重定向，即：301永久性重定向和302临时性重定向。实施301后，新网址完全继承旧网址，旧网址的排名等完全清零；实施302后，对旧网址没有影响，但新网址不会有排名。]]></description>
			<content:encoded><![CDATA[<p>重定向就是将网页自动转向重定向，即：301永久性重定向和302临时性重定向。实施301后，新网址完全继承旧网址，旧网址的排名等完全清零；实施302后，对旧网址没有影响，但新网址不会有排名。<br />
　　<br />
　　301 Redirect 永久重定向的实现<br />
　　<br />
　　在我们的网站建设中，时常会遇到需要网页重定向的情况：象网站调整，如改变网页目录结构，网页被移到一个新地址，再或者，网页扩展名改变，如因应用需要把.php改成.Html或.shtml，在这种情况下，如果不做重定向，则用户收藏夹或搜索引擎数据库中旧地址只能让访问客户还会得到一个404页面错误信息，访问流量白白丧失；再如某些注册了多个域名的网站，也需要通过重定向让访问这些域名的用户自动跳转到主站点，等等。<br />
　　<br />
　　常用的重定向方式有: 301 redirect、302 redirect 与 meta fresh：<br />
　　<br />
　　301 redirect——301代表永久性转移(Permanently Moved)，301重定向是网页更改地址后对搜索引擎友好的最好方法，只要不是暂时搬移的情况,都建议使用301来做转址。<br />
　　<br />
　　302 redirect——302代表暂时性转移(Temporarily Moved )，在前些年，不少Black Hat SEO曾广泛应用这项技术作弊，目前，各大主要搜索引擎均加强了打击力度，象Google前些年对Business.com以及近来对BMW德国网站的惩罚。即使网站客观上不是spam，也很容易被搜寻引擎容易误判为spam而遭到惩罚。<br />
　　<br />
　　meta fresh——这在2000年前比较流行，不过现在已很少见。其具体是通过网页中的meta指令，在特定时间后重定向到新的网页，如果延迟的时间太短(约5秒之內)，会被判断为spam。</p>
<p>PHP实现301重定向：<br />
我原来的用的是Win主机，域名为&lt;a href=”http://blog.woodfeed.com”&gt;http://blog.woodfeed.com&lt;/a&gt;，用404实现的伪静态链接，所以真是域名为&lt;a href=”http://blog.woodfeed.com/404;http://blog.woodfeed.com:80/****/***.html”&gt;http://blog.woodfeed.com/404;http://blog.woodfeed.com:80/****/***.html&lt;/a&gt;样式的。现在要更改为：&lt;a href=”http://woodfeed.com/XXXXX”&gt;http://woodfeed.com/XXXXX&lt;/a&gt;，即直接加文章名的样式。<br />
具体显示如下：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span>p
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;HTTP/1.1 301 Moved Permanently&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$string1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'QUERY_STRING'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//取出域名http://blog.woodfeed.com后面的字符串</span>
<span style="color: #000088;">$string2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;404;http://blog.woodfeed.com:80/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$string1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 将其中的404内容部分删除</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.html&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$string2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//去除后面的 .html</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: http://woodfeed.com/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//终止掉程序的运行</span>
<span style="color: #339933;">--&gt;</span></pre></div></div>

<p><strong>将以上代码加到打开网页时最先执行的php文件中，比如WordPress中当前使用主题的header.php</strong></p>
<h2  class="related_post_title">相关文章</h2><ul class="related_post"><li><a href="http://woodfeed.com/apache-mod_rewrite-for-301-redirect/" title="Apache mod_rewrite 实现301重定向">Apache mod_rewrite 实现301重定向</a><br /><small>它提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求。它支持每个完整规则可以拥...</small></li></ul>]]></content:encoded>
			<wfw:commentRss>http://woodfeed.com/php-code-for-301-redirect/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
