Apache的Mod_Rewrite模块设置

 利用apache的mod_rewrite模块的全套重定向方案

首先介绍一下怎样打开apache的mod_rewrite功能。

首先来重新编译安装apache-2.0.59

#tar -zxvf httpd-2.0.59.tar.gz
#cd httpd-2.0.59
#./configure –prefix=/opt/apache2 –enable-modules=so –enable-module=rewrite –enable-shared=rewrite –enable-shared=max
#make
#make install

apache安装完毕,下面来加载编译mod_rewrite.so这个模块。

#cd /soft/httpd-2.0.59/modules/mappers
#/opt/apache2/bin/apxs -c mod_rewrite.c -lgdbm
#gcc -shared -o mod_rewrite.so mod_rewrite.o -lgdbm
#/opt/apache2/bin/apxs -i -A -n rewrite mod_rewrite.so

最近,项目需要在已经编译好的Apache上以动态方式加载rewrite模块。于是,我们在网上找了许多资料,但都讲解得不是很详细且格式错位问题严重。所以我们有必要把这一过程再重述一遍。实际操作步骤如下:

工作现场描述:
Linux 2.4.21
apache_1.3.34.tar.gz 解压后的目录为 /root/apache_1.3.34 文中简称为“源码目录”
apache安装目录 /usr/local/apache 文中简称为“目标目录”

步骤:
1、Apache安装rewrite模块的时候需要DBM支持,否则无法编译,所以首先要安装一个GDBM
下载地址:[url]ftp://ftp.gnu.org/gnu/gdbm/[/url]
安装步骤:
进入安装目录,
./configure
make
make install
make install-compat (最后行也要执行。否则无法编译出ndbm.h头文件)
如果您不能确定服务器上是否已经装有DBM,那么可以安装一下。否则这步可以跳过。

2、现在到apache源码目录的标准模块目录中(/root/apache_src/src/modules/standard/)中,使用如下指令编译出so文件:
/usr/local/apache/bin/apxs -c mod_rewrite.c -lgdbm
即可得到mod_rewrite.so文件。
备注:"-lgdbm"是用为说明在编译mod_rewrite.c时要把gdbm链接进来。这样在第6步启动apache时就不会报出"dbm fetch"的错误了。

3、现在让apache的apxs来自动向http.conf配置文件中加入LoadModule语句并将mod_rewrite.so文件拷贝到apache/libexec目录
/usr/local/apache/bin/apxs -i -A -n rewrite /root/apache_1.3.34/src/modules/standard/mod_rewrite.so
备注:命令中的rewrite参数是告诉apxs命令mod_rewrite.so文件中的模块名。在命令执行后,apxs会在LoadModule中为 rewrite加上"_module"以标名模块名称。如果你在启动apache时发现总是给出“不能定位API”之类的错误,那就是说明 LoadModule后面的模块名的语法要根据您的apache版本加以改变。

4、停止apache
apache/bin/apachectl stop

5、运行配置文件检查命令
apache/bin/apachectl configtest
如显示Syntax OK,则表示整个操作成功。那么可以到第6步。否则根据提示信息进行调试。但只要按照此文所说进行操作是不会出错的。

6、启动apache
apache/bin/apachectl start

现在查看phpinfo的信息

Loaded Modules mod_dir mod_imap mod_actions mod_userdir mod_alias mod_rewrite mod_so sapi_apache2

表明我的机器就支持rewrite了,但是我的dz怎么不能静态呢?
要修改以下配置
1.在配置文件(通常就是 conf/httpd.conf)中加入如下代码

RewriteEngine On
RewriteRule ^(.*)/archiver/([a-z0-9-]+.html)$ $1/archiver/index.php?$2
RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3
RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page/%3D$4&page=$3
RewriteRule ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3

RewriteEngine On
RewriteRule ^archiver/([a-z0-9-]+.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+).html$ forumdisplay.php?fid=$1&page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+).html$ viewthread.php?tid=$1&extra=page/%3D$3&page=$2
RewriteRule ^profile-(username|uid)-(.+).html$ viewpro.php?$1=$2

保存。
2.检查论坛所在目录中是否存在 .htaccess 文件,如果不存在,请手工建立此文件。文件内容为:

RewriteEngine On
# 修改以下语句中的 /discuz 为你的论坛目录地址,如果程序放在根目录中,请将 /discuz 修改为 /
RewriteBase /

RewriteRule ^archiver/([a-z0-9-]+.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+).html$ forumdisplay.php?fid=$1&page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+).html$ viewthread.php?tid=$1&extra=page/%3D$3&page=$2
RewriteRule ^profile-(username|uid)-(.+).html$ viewpro.php?$1=$2

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
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
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
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

查询字符串的重定向写法用“RewriteCond %{QUERY_STRING}”

Module mod_rewrite:Rewriting URLs With Query Strings和mod rewrite query string problem

本站文章除注明转载外,均为本站原创
转载请注明:

本文链接:http://www.1m1.org/blog/post/apache-mod-rewrite-setting/
来自:真实就是价值

你可能感兴趣的:(Linux,apache,.htaccess,redirect,query,string,wordpress)