Java堂  


实现WordPress的Permalinks

归档在: Tools, Web&Server — Jet @ 11:14 下午
原文出处: http://www.javatang.com/archives/2006/03/14/14246.html
作者: Jet Mah from Java堂
声明: 可以非商业性任意转载, 转载时请务必以超链接形式标明文章原始出处、作者信息及此声明!

之前了解过WordPress内置Permalinks功能,使用Apache的Rewrite实现的动态页面静态化。之前也接触过Apache的Rewrite,但是最后查阅官方的文档才发现Wordpress原来如此的简单。 ^_^!!
实现的过程如下:
1. 打开Apache的Rewrite模块。默认是注释掉的,去掉注释就可以了。

  1. LoadModule rewrite_module "modules/mod_rewrite.so"

2. 设置主机目录的AllowOverride 为All。虽然可以直接设置所有的目录AllowOverride为All,但是基于安全性的考虑,还是单独设置比较好。如下所示:

  1. <virtualhost 127.0.0.1:80>
  2.   ServerName www.yourdomain.com
  3.   DocumentRoot "/usr/wwwroot/mysite"
  4.   <directory "/usr/wwwroot/mysite">
  5.     AllowOverride All
  6.   </directory>
  7. </virtualhost>

3. 进入WP后台的”Options”-”Permalink “选项,设置Structure。比如:/%year%/%monthnum%/%day%/%postname%/
其中具体的参数含义分别如下:

  1. %year%
  2.   The year of the post, four digits, for example 2004
  3. %monthnum%
  4.   Month of the year, for example 05
  5. %day%
  6.   Day of the month, for example 28
  7. %hour%
  8.   Hour of the day, for example 15
  9. %minute%
  10.   Minute of the hour, for example 43
  11. %second%
  12.   Second of the minute, for example 33
  13. %postname%
  14.   A sanitized version of the title of the post. So “This Is A Great Post!‿ becomes “this-is-a-great-post‿ in the URI (see note below)
  15. %post_id%
  16.   The unique ID # of the post, for example 423
  17. %category%
  18.   A sanitized version of the category name. Nested sub-categories appear as nested directories in the URI.
  19. %author%
  20.   A sanitized version of the author name.

经过以上三步之后,就可以实现动态网址静态化了。

说明两点:
1) 开始的时候我以为必须自己来写RewriteRule,后来看到官方说明原来是在WP后台对Permalink 进行设置之后自动生成的。
2) 算是个插曲吧。因为开始以为必须自己写.htaccess的内容,所以首先要创建.htaccess文件。这个在Linux下面是很容易的,但是在Windows下面是无法直接创建的。实现的方法有两种,一是使用UtralEdit创建新文件,然后保存成.htaccess;二是首先创建一个文本文件,然后在cmd中使用rename更名为.htaccess。

good luck!

参考资料:
WordPress官方文档 Using Permalinks
http://codex.wordpress.org/Using_Permalinks