WordPress 和 mod_rewrite|WordPress & mod_rewrite

原来我的wordpress是放在/Huizhe_wordpress下,然后我ln -s到/blog,用户需要从leen.name/blog访问。

Previously my wordpress is under path /Huizhe_wordpress, then I “ln -s” it to /blog, so users need to visit it by leen.name/blog.

前几天在做wordpress theme的时候无意中发现原来wordpress不一定要从当前目录访问的,可以把它的index.php copy到根目录/下,然后把其中的

Several days before, when I was making a wordpress theme, I found that it does not need to be in the same directory to visit wordpress. Copy it’s index.php from /Huizhe_wordpress to /, and modify the line:

require('./wp-blog-header.php');

改成

to:

require('./Huizhe_wordpress/wp-blog-header.php');

这样就可以在不弄乱根目录的情况下让url看起来更干净。——我的leen.name/blog变成了leen.name。

Then, we can make the wordpress url cleaner, without messing up the root folder. my wordpress url changed from leen.name/blog to leen.name.

在改变index.php后,wordpress则会自动把根目录/下的.htaccess加上

After changing index.php, wordpress will add these lines to .htaccess file under root folder automaticlly:

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

但这样改后,有2个问题:

But there are two problems here:

  1. 原来的图像都没了,类似leen.name/blog/wp-content/xxx/xx/xxx.jpg的地址会被当成一篇不存在的blog。
  2. 原来的blog链接失效。类似leen.name/blog/post-name。
  1. the image links all become invalid, images like “leen.name/blog/wp-content/xxx/xxx/xxx.jpg” will be treated as a un-existing blog post.
  2. the old blog links become invalid too, like “leen.name/blog/post-name”

在sam的帮助下看了一下apache的mod_rewrite,在.htaccess前加上这样四行,问题解决。

With Sam’s help, I read some document about apache’s mod_rewrite. The 2 problems could be resolved by just insterting 4 lines of .htaccess:

RewriteCond %{REQUEST_URI} ^/blog/.*.(jpg|jpeg|png|gif|js)$ [NC]
RewriteRule ^/blog/(.*)$ /Huizhe_wordpress/$1 [L,NC]
RewriteCond %{REQUEST_URI} ^/blog/? [NC]
RewriteRule ^/blog/?(.*)$ /$1 [R,L,NC]

又知道了一些新东西,世界真美好。

Yeah~