一般在安装Apache时已经包含了mod_rewrite模块。默认是不启动的,这时我们需要手动启用它。
查看apache的httpd.conf没有LoadModule rewrite_module modules/mod_rewrite.so这一行,则需要手动生成此模块,方法如下:
apache源码包路径为:/ftp/httpd2.2
apache安装路径:/usr/local/apache/
一、增加mod_rewrite模块
# find . -name mod_rewrite.c //在apache的源码安装目录中寻找mod_rewrite.c文件
./modules/mappers/mod_rewrite.c
# cd /ftp/httpd2.2/modules/mappers //进入包含mod_rewrite.c文件的目录
# /usr/local/apache/bin/apxs -c mod_rewrite.c //apxs应指定绝对路径,在你当前正在使用apache的bin目录里
# /usr/local/apache/bin/apxs -i -a -n mod_rewrite mod_rewrite.la
如果没有什么错误的话,应该在你的apache的modules目录(/usr/local/apache/modules)中编译出一个mod_rewrite.so文件。
编辑httpd.conf文件,确认httpd.conf中已经包含mod_rewrite.so的加载语句,如下:
LoadModule mod_rewrite_module modules/mod_rewrite.so
二、重启apache
1、如果出现以下错误:
httpd: Syntax error on line 54 of /usr/local/apache/conf/httpd.conf: Can't locate API module structure `mod_rewrite_module' in file /usr/local/apache/modules/mod_rewrite.so: /usr/local/apache/modules/mod_rewrite.so: undefined symbol: mod_rewrite_module
需要把
LoadModule mod_rewrite_module modules/mod_rewrite.so
改为
LoadModule rewrite_module modules/mod_rewrite.so
2、如果出现以下错误
httpd: Syntax error on line 54 of /usr/local/apache/conf/httpd.conf: module rewrite_module is built-in and can't be loaded
表示模块是内建的,不用再调入,注释掉
#LoadModule rewrite_module modules/mod_rewrite.so
三、对某一虚拟站点开启url重写
1、编辑httpd-vhost.conf,在对应的站点标签内加入:
<Directory "/website/webA">
AllowOverride All
</Directory>
即:
<VirtualHost *:80>
DocumentRoot "/website/webA"
ErrorLog "logs/webA.com-error_log"
CustomLog "logs/webA.com-access_log" common
<Directory "/website/webA">
AllowOverride All
</Directory>
</VirtualHost>
四、把对站点的重写规则保存到站点根目录下,文件名为“.htaccess”