最近工作需要用到apache rewrite功能,之前本机已经安装apache,但没有安装rewrite模块。下面总结一下rewrite模块的安装过程和相关知识。本机已安装的apache版本为2.2.32,操作系统为centos6.5.
一、 安装过程
1. 在apache源码目录下查找mod_rewrite.c文件。
find . -name mod_rewrite.c
2. 进入mod_rewrite.c文件所在目录。
cd modules/mappers
3. 将C程序编译成共享模块,以便在运行时加载到apache服务器中。
/usr/local/apache2/bin/apxs -cmod_rewrite.c
其中/usr/local/apache2为本机的apache安装目录。
console输出如下:
/usr/local/apache2/build/libtool --silent--mode=compile gcc -prefer-pic -DLINUX-D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/usr/local/apache2/include -I/usr/local/apache2/include -I/usr/local/apache2/include -c -o mod_rewrite.lo mod_rewrite.c&& touch mod_rewrite.slo
/usr/local/apache2/build/libtool --silent--mode=link gcc -o mod_rewrite.la -rpath/usr/local/apache2/modules -module -avoid-version mod_rewrite.lo
4. 安装动态共享对象到服务器的module目录中,并修改httpd.conf文件配置。
/usr/local/apache2/bin/apxs -i -a -n rewritemod_rewrite.la
console输出如下:
/usr/local/apache2/build/instdso.sh SH_LIBTOOL='/usr/local/apache2/build/libtool'mod_rewrite.la /usr/local/apache2/modules
/usr/local/apache2/build/libtool --mode=installcp mod_rewrite.la /usr/local/apache2/modules/
libtool: install: cp .libs/mod_rewrite.so/usr/local/apache2/modules/mod_rewrite.so
libtool: install: cp .libs/mod_rewrite.lai/usr/local/apache2/modules/mod_rewrite.la
libtool: install: cp .libs/mod_rewrite.a/usr/local/apache2/modules/mod_rewrite.a
libtool: install: chmod 644/usr/local/apache2/modules/mod_rewrite.a
libtool: install: ranlib/usr/local/apache2/modules/mod_rewrite.a
libtool: finish:PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/sbin"ldconfig -n /usr/local/apache2/modules
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/apache2/modules
If you ever happen to want to link againstinstalled libraries
in a given directory, LIBDIR, you must eitheruse libtool, and
specify the full pathname of the library, or usethe '-LLIBDIR'
flag during linking and do at least one of thefollowing:
- addLIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- addLIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- usethe '-Wl,-rpath -Wl,LIBDIR' linker flag
- haveyour system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation aboutshared libraries for
more information, such as the ld(1) and ld.so(8)manual pages.
----------------------------------------------------------------------
chmod 755/usr/local/apache2/modules/mod_rewrite.so
[activating module `rewrite' in/usr/local/apache2/conf/httpd.conf]
参数含义:
-i 表示需要执行安装操作。
-a 自动增加一个LoadModule行到httpd.conf文件中,以激活此模块,或者,如果此行已经存在,则启用之。
-n 增加或启用的模块名称。
5. 打开apache安装目录下的conf/httpd.conf文件,检查是否有如下行:
LoadModule rewrite_module modules/mod_rewrite.so
如果包含此行,则安装成功。
6. 重新启动apache。
/usr/local/apache2/bin/apachectl stop
/usr/local/apache2/bin/apachectl start
二、 安装错误解决方法
1. 现象: Can`t loacte API module staructure`mod_rewrite_module` in file
/usr/local/apache2/modules/mod_rewrite.so:/usr/local/apache2/lib/libapr-
0.so.0:undefined symbol:mod_rewrite_module
解决方法: httpd.conf里面写的mod_rewrite_module改成rewrite_module。
2. 现象: module rewrite_module is built-in and can't beloaded.
解决方法: 表示模块是内建的,不用再手动调入,注释掉httpd.conf中如下行:
#LoadModule rewrite_modulemodules/mod_rewrite.so
检查所有内建模块的命令: apachectl -l
三、 rewrite功能简介
需求: 如果本地包含样式文件,则通过本地apache访问资源;否则从远端服务器获取资源。
实现:
DocumentRoot"/home/zhouyingbo/sourcecode/style_other/20111031_88209_punish_1"
ServerName style.china.alibaba.com
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f #不是本地文件夹
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d #不是本地文件
RewriteRule ^(.*)$ http://10.20.136.137$1 [L] #重写到远端服务器
说明:
RewriteEngine On 表示开启重写引擎功能;
RewriteCond语法格式为: RewriteCond TestString CondPattern,当TestString满足CondPattern时,执行RewriteRule定义的规则。
RewriteRule语法格式为: RewriteRule Pattern Substitution,Pattern为posix正则式,Substitution用来替换相应的匹配。
四、参考资料
RewriteCond参考文档:http://blog.csdn.net/keyunq/article/details/2536875
RewriteRule参考文档:http://blog.csdn.net/bjbs_270/article/details/2097064