学习日记利用apache的mod_rewrite模块的全套重定向方案

学习日记网站经过近3年的运行,其间改变过几次域名和网址结构,造成一些旧的网址不能访问。过去是jsp版本的程序,在程序中可以自由的进行重定向。但是,当我把数据移到wordpress中时,我才发现在apache服务器下的mod_rewrite模块是如此强大。不用程序,所有的重定向都可以在apache的配置文件中做好(我这里是在.htaccess文件中),甚至是把现有系统不存在的、带查询字符串的网址进行自由的301重定向。下面是学习日记Blog全套重定向方案。供自己备忘和需要的朋友参考。

1、根目录“/”下:

# BEGIN WordPress

RewriteEngine On
RewriteBase /

#把learndiary.com的网址全部重定向到www.learndiary.com下
RewriteCond %{HTTP_HOST} ^learndiary\.com [NC]
RewriteRule ^(.*)$ http://www\.learndiary\.com/$1 [L,R=301]

#除开*.do形式的URL(必须),其它是wordpress添加的用来实现动态网址伪静态化的,具体实现原理不懂
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.+\.do
RewriteRule . /index.php [L]

#把*.do形式的带查询字符串的URL重定向现在的网址
RewriteCond %{REQUEST_URI} dis(Goal|Diary)ContentAction\.do
RewriteCond %{QUERY_STRING} ^((([a-zA-Z]+)=([0-9a-z]*)&)*)goalID=([0-9]+)((&([a-zA-Z]+)=([0-9a-z]*))*)$
RewriteRule ^dis(Goal|Diary)ContentAction\.do$ http://www.learndiary.com/archives/diaries/%5.htm? [R=301]

RewriteCond %{REQUEST_URI} rssAction\.do
RewriteCond %{QUERY_STRING} ^type=latestDiaries(.+)$
RewriteRule ^rssAction\.do$ http://www.learndiary.com/feed? [R=301]

RewriteCond %{REQUEST_URI} rssAction\.do
RewriteCond %{QUERY_STRING} ^type=latestAdvices(.+)$
RewriteRule ^rssAction\.do$ http://www.learndiary.com/comments/feed? [R=301]

# END WordPress

2、在/archives/goals/目录下,把下面的URL重定向到/archives/diaries/下面:

# Control the page under /archives/goals/ 301 redirect to /archives/diaries/
# This .htaccess is puted into /archives/goals/

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www\.learndiary\.com/archives/diaries/$1 [R=301]

# End
# 2007.07.14 10:40

3、在目录/diaries/下,把/diaries/103.jsp形式的URL重定向到/archives/diaries/103.htm

# Control the page like /diaries/103.jsp 301 redirect to /archives/diaries/103.htm
# This .htaccess is puted into /diaries/

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)\.jsp$ http://www\.learndiary\.com/archives/diaries/$1.htm [R=301]

# End
# 2007.07.14 11:34

4、在目录/goals/下,把/goals/1.jsp形式的URL重定向到/archives/diaries/1.htm

# Control the page like /goals/1.jsp 301 redirect to /archives/diaries/1.htm
# This .htaccess is puted into /goals/

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)\.jsp$ http://www\.learndiary\.com/archives/diaries/$1.htm [R=301]

# End
# 2007.07.14 11:45

5、放在/java目录下,把域名java.learndiary.com或www.java.learndiary.com下的所有网址重定向到www.learndiary.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^java.learndiary.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.java.learndiary.com$
RewriteRule ^(.*)$ http://www.learndiary.com/$1 [R=301,L]

6、在目录/sitemaps/下,把/sitemaps/goal1-1.jsp形式的网址重定向到/archives/sitemaps/goal1-1.htm

# Control the page like /sitemaps/goal1-1.jsp 301 redirect to /archives/sitemaps/goal1-1.htm
# This .htaccess is puted into /sitemaps/

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(goal[0-9]+\-1)\.jsp$ http://www\.learndiary\.com/archives/sitemaps/$1\.htm [R=301]
RewriteRule ^(goals-1)\.jsp$ http://www\.learndiary\.com/archives/sitemaps/$1\.htm [R=301]

# End
# 2007.07.14 12:00

其中,带查询字符串的重定向写法很不好找,花了我好多天时间,问了三个论坛,查了N多网页也没有结果。有朋友还下了一个结论:“除非用程序,否则不可能实现这个需求”。最后用“RewriteCond %{QUERY_STRING}”为关键字在google.com上搜索,才在Module mod_rewrite:Rewriting URLs With Query Stringsmod rewrite query string problem找到一点线索,花了一个下午来调试才成功的。得来不易啊。

我对apache的mod_rewrite并不熟悉,以上有些内容自己也是从别人那里抄来的,道理还不是很明白,也许还有错误。希望发现错误和需要改进的地方的朋友提示一下,谢谢。

14 thoughts on “学习日记利用apache的mod_rewrite模块的全套重定向方案”

  1. RewriteCond %{REQUEST_FILENAME} !-f
    中-f是指判断是否在硬盘上存在该文件,!-f表示不存在该文件时,你说的不懂是指这个么

  2. 谢谢。

    我主要不懂的地方是:
    wordpress自己自动创建的.htaccess文件:

    # BEGIN WordPress

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

    # END WordPress

    怎么就能够实现伪静态网址?

    而且:
    1、在地址里显示的是伪静态网址,而不是真实的带查询参数的网址;
    2、访问这个伪静态网址返回的是200 OK码;

    我估计它是在程序的什么地方实现的,但没有去进一步学习。于是就存疑在这里了。

  3. 博客平台迁移带查询的重定向遇到些问题,特请教
    原地址a.asp?id=12 要重定向到 b.php?id=12
    现在只做到一步,就是a.asp可以重定向到b.php....
    但是查询的参数就没有传递了....

    RewriteCond %{QUERY_STRING} .
    RewriteRule ^a\.asp$ b.php?$1 [R=301]

    初次使用重定向...不太明白,希望能得到你的帮助...谢谢
    QQ:85508221 Mail:49degree#GMail.com

  4. 站长您好...您帮忙写的规则可以用了....
    非常感谢.....:)不知道可否交个朋友..谢谢啊

  5. Pingback: pingback

Comments are closed.