<?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>Kangzj &#187; rewrite</title>
	<atom:link href="http://kangzj.net/tag/rewrite/feed/" rel="self" type="application/rss+xml" />
	<link>http://kangzj.net</link>
	<description>记录技术和生活~</description>
	<lastBuildDate>Mon, 07 May 2012 01:03:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Rewrite和Redirect的区别与联系</title>
		<link>http://kangzj.net/difference-between-rewrite-and-redirect/</link>
		<comments>http://kangzj.net/difference-between-rewrite-and-redirect/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 01:35:38 +0000</pubDate>
		<dc:creator>kangzj</dc:creator>
				<category><![CDATA[网站制作]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[rewrite]]></category>

		<guid isPermaLink="false">http://kangzj.net/?p=846</guid>
		<description><![CDATA[我们经常会用到这两个概念，大家对他们的功能已经很熟悉了，这篇文章以实例说明Rewrite和Redirect的区别与联系，]]></description>
			<content:encoded><![CDATA[<p>看到这个题目，希望大家不要头疼，呵呵，有点像政治里的简答题吧，呵呵，我故意的:-)</p>
<p>我们经常会用到这两个概念，大家对他们的功能已经很熟悉了，我罗嗦下，不过肯定有你不知道的地方哦：</p>
<p><strong>URL Rewrite</strong>：写在.htaccess中（当然也可以写在Apache的主配置文件中），把不好看的、带一堆参数的URL利用正则表达式变成很友好的URL，WordPress的固定链接就是URL Rewrite实现的，可以参考我的URL:-)，下面是WordPress的.htaccess（意义：如果不是存在的文件或者目录，就Rewrite到index.php处理）：</p>
<p><span id="more-846"></span></p>
<blockquote><p># BEGIN WordPress<br />
&lt;IfModule mod_rewrite.c&gt;<br />
RewriteEngine On<br />
RewriteBase /<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteRule . /index.php [L]<br />
&lt;/IfModule&gt;<br />
# END WordPress</p></blockquote>
<p><strong>Redirect</strong>：可以写在.htaccess中，也可以用动态语言（php,jsp等）来发送相关Header信息。多用于URL发生改变时，将旧的URL定位到新的URL。Redirect有三种：301，302，303。这几种信号对用户来说没有什么意义，他们看到效果只是一个跳转，浏览器中旧的URL变成了新的URL。所以它们主要是对搜索引擎的，告诉搜索引擎，这个跳转的含义是什么。</p>
<blockquote><p>301：永久重定向。</p>
<p>302：临时重定向（http 1.0）；Found(http 1.1)。</p>
<p>303：对于POST请求，它表示请求已经被处理，客户端可以接着使用GET方法去请求Location里的URI。</p>
<p>307：对于POST请求，表示请求还没有被处理，客户端应该向Location里的URI重新发起POST请求。</p></blockquote>
<p>话说现在已经是普及http 1.1的时代了，所以，302已经不是大家常说的临时重定向的含义了，它表示Found，什么意思呢？——就是找到了相关的URL，然后给你定位到相应的URL。而我们平时说的临时重定向应该是303或者307了。下面是用php Redirect的例子：</p>
<blockquote><p>header(&#8216;HTTP/1.1 303 See Other&#8217;);<br />
header(&#8216;Location: http://localhost/&#8217;);</p></blockquote>
<p>用.htaccess来Redirect的例子：</p>
<blockquote><p>Redirect /wordpress http://yourdomain.com/blog</p></blockquote>
<p>这种情况默认是用302重定向的。</p>
<p>还有一种情况，用Rewrite这样写：</p>
<blockquote><p>RewriteRule ^wordpress(.*)$ /blog$1 [R=301,L]</p></blockquote>
<p>奇怪吧，Rewrite也可以用来做Redirect。只不过这里Rewrite跟平时的Rewrite就不一样了，浏览器中URL会发生改变。</p>
<p>说了这么多，应该说清楚了，打完收工:-)</p>
<p>有问题请留言，说的不对的请指正。</p>
<p style='text-align:left'>&copy; 2009, <a href='http://kangzj.net'>kangzj</a>. 版权所有.  </p>
<hr />
<p><small>© kangzj for <a href="http://kangzj.net">Kangzj</a>, 2009. |
<a href="http://kangzj.net/difference-between-rewrite-and-redirect/">http://kangzj.net/difference-between-rewrite-and-redirect/</a> |
<a href="http://kangzj.net/difference-between-rewrite-and-redirect/#comments">17 条评论</a> |
Add to
<a href="http://del.icio.us/post?url=http://kangzj.net/difference-between-rewrite-and-redirect/&title=Rewrite和Redirect的区别与联系">del.icio.us</a>
<br/>
Post tags: <a href="http://kangzj.net/tag/redirect/" rel="tag">redirect</a>, <a href="http://kangzj.net/tag/rewrite/" rel="tag">rewrite</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://kangzj.net/difference-between-rewrite-and-redirect/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>关于“永久链接”/“固定链接”和301重定向</title>
		<link>http://kangzj.net/about-permanent-link-and-301/</link>
		<comments>http://kangzj.net/about-permanent-link-and-301/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 16:18:05 +0000</pubDate>
		<dc:creator>kangzj</dc:creator>
				<category><![CDATA[独立博客]]></category>
		<category><![CDATA[301重定向]]></category>
		<category><![CDATA[permanent link]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rewrite]]></category>
		<category><![CDATA[sablog]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[固定链接]]></category>
		<category><![CDATA[永久链接]]></category>

		<guid isPermaLink="false">http://kangzj.net.ru/?p=133</guid>
		<description><![CDATA[原来我不是很理解“永久链接”的意思，在转换了博客的程序之后，就完全理解了：永久链接也叫固定链接就是在你的网站程序发生了变化之后，只要保证永久链接不变，大家还能通过搜索引擎或者超链接访问到你的文章]]></description>
			<content:encoded><![CDATA[<p>    看到一篇关于“<strong>永久链接</strong>”的文章，觉得写得不错，这里介绍给大家，并且把我应用中的一些问题也给写一下。</p>
<p>    这个是原文：<a href="http://blogunion.org/wordpress/wordpress-tips/url-setting-tips.html">http://blogunion.org/wordpress/wordpress-tips/url-setting-tips.html</a>，主要写了这几点：</p>
<li>     (1) 不要让日期出现在<strong>永久链接</strong>里面;</li>
<li>     (2) 不要让分类的链接出现在<strong>永久链接</strong>里面;</li>
<li>     (3) <strong>永久链接</strong>不要过深;</li>
<li>     (4) 不要让中文字符出现在<strong>永久链接</strong>里面;</li>
<li>     (5) 好的<strong>永久链接</strong>形式是:域名/文章名;</li>
<p><span id="more-133"></span></p>
<p>    原来我不是很理解“<strong>永久链接</strong>”的意思，在转换了博客的程序之后，就完全理解了：<strong>永久链接</strong>也叫<strong>固定链接</strong>就是在你的网站程序发生了变化之后，只要保证<strong>永久链接</strong>不变，大家还能通过搜索引擎或者超链接访问到你的文章。</p>
<p>    以我的博客为例，我之前用的程序是sablog开启了<strong>永久链接</strong>，每篇文章都有一个<strong>永久链接</strong>，在转换数据的时候，把这个<strong>永久链接</strong>也加入了wordpress的数据库，这样就保证了文章的地址不变，不会影响原有文章的访问。</p>
<p>    但是sablog的archiver的文章的url是 http://kangzj.net.ru/archiver/?xxx.html 的形式而不是 http://kangzj.net.ru/xxx/ 的形式，如果用这样的地址来访问新网站，自然会得到一个“404”。怎么办呢——还好，我们的wordpress开启了rewrite，也就是所有的请求都是交给index.php来处理的，这就好办了，通过判断访问的URI把访问较多的文章用“<strong>301重定向</strong>”定到该文章的<strong>固定链接</strong>地址就OK了，比如：</p>
<pre lang="php">$request_uri=$_SERVER[''REQUEST_URI];
switch($request_uri)
{
    case '/achiver/?xxxx.html':
      $request_uri='/abc/';
      break;
    default:
      .....;
}
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://kangzj.net.ru'.$request_uri);
exit;</pre>
<p>    下面是我的网站正在用的（因为网站之前还用过别的域名，所以还加了域名的<strong>301重定向</strong>），加在wordpress的index.php最前面的代码，把以前网站的好多有流量的但是现在丢失了的链接给“<strong>301重定向</strong>”到新的“<strong>永久链接</strong>”：</p>
<pre lang="php">//如果是别的域名，永久转向kangzj.net.ru
$host = $_SERVER['HTTP_HOST'];
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$flag=0;
switch ($request_uri)
{
	case '/read.php/65.html':
		$request_uri = '/linux-start-on-boot/';
		break;
	case '/read.php/65.htm':
		$request_uri = '/linux-start-on-boot/';
		break;
	case '/read.php/76.htm':
		$request_uri = '/fckeditor-not-proper-response/';
		break;
	case '/archiver/?article-55.html':
		$request_uri = '/asus-x81-ar2009-ar928x-driver/';
		break;
	case '/archiver/?article-63.html':
		$request_uri = '/different-copiler-error/';
		break;
	case '/archiver/?article-72.html':
		$request_uri = '/zoj-1002/';
		break;
	case '/archiver/?article-59.html':
		$request_uri = '/fckeditor-not-proper-response/';
		break;
	case '/archiver/?article-51.html':
		$request_uri = '/linux-for-human/';
		break;
	case '/archiver/?article-47.html':
		$request_uri = '/squid-reverse-proxy/';
		break;
	case '/archives/65/':
		$request_uri = '/dynamic-ip-as-server/';
		break;
	case '/archives/46/':
		$request_uri = '/apache-ipv6-proxy-3/';
		break;
	case '/archives/45/':
		$request_uri = '/iis6-ipv6/';
		break;
	case '/archives/44/':
		$request_uri = '/apache-ipv6-proxy-2/';
		break;
	case '/date/200906/':
		$request_uri = '/2009/06/';
		break;
	case '/archives/73/':
		$request_uri = '/poj-2000/';
		break;
	default:
		$flag=1;
		break;
}		

if($host!='kangzj.net.ru' || $flag==0)
{

	header('HTTP/1.1 301 Moved Permanently');
	header('Location: http://kangzj.net.ru'.$request_uri);
	exit;
}
//--------------------------------------</pre>
<p>    做完之后才发现，有一个链接竟然很火，这一天从百度就来了将近40个IP，嗯，多亏做了下，要不损失大了。</p>
<p style='text-align:left'>&copy; 2009, <a href='http://kangzj.net'>kangzj</a>. 版权所有.  </p>
<hr />
<p><small>© kangzj for <a href="http://kangzj.net">Kangzj</a>, 2009. |
<a href="http://kangzj.net/about-permanent-link-and-301/">http://kangzj.net/about-permanent-link-and-301/</a> |
<a href="http://kangzj.net/about-permanent-link-and-301/#comments">5 条评论</a> |
Add to
<a href="http://del.icio.us/post?url=http://kangzj.net/about-permanent-link-and-301/&title=关于“永久链接”/“固定链接”和301重定向">del.icio.us</a>
<br/>
Post tags: <a href="http://kangzj.net/tag/301%e9%87%8d%e5%ae%9a%e5%90%91/" rel="tag">301重定向</a>, <a href="http://kangzj.net/tag/permanent-link/" rel="tag">permanent link</a>, <a href="http://kangzj.net/tag/php/" rel="tag">php</a>, <a href="http://kangzj.net/tag/rewrite/" rel="tag">rewrite</a>, <a href="http://kangzj.net/tag/sablog/" rel="tag">sablog</a>, <a href="http://kangzj.net/tag/wordpress/" rel="tag">wordpress</a>, <a href="http://kangzj.net/tag/%e5%9b%ba%e5%ae%9a%e9%93%be%e6%8e%a5/" rel="tag">固定链接</a>, <a href="http://kangzj.net/tag/%e6%b0%b8%e4%b9%85%e9%93%be%e6%8e%a5/" rel="tag">永久链接</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://kangzj.net/about-permanent-link-and-301/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

