apache rewrite模块安装和使用

最近工作需要用到apacherewrite功能,之前本机已经安装apache,但没有安装rewrite模块。下面总结一下rewrite模块的安装过程和相关知识。本机已安装的apache版本为2.2.16,操作系统为ubuntu10.10。

一、安装过程

1.在apache源码目录下查找mod_rewrite.c文件。

find.-namemod_rewrite.c

2.进入mod_rewrite.c文件所在目录。

cdmodules/mappers

3.将C程序编译成共享模块,以便在运行时加载到apache服务器中。

/usr/alibaba/install/httpd-2.2.16/bin/apxs-cmod_rewrite.c

其中/usr/alibaba/install/httpd-2.2.16为本机的apache安装目录。

console输出如下:

/usr/alibaba/install/httpd-2.2.16/build/libtool--silent--mode=compilegcc-prefer-pic-DLINUX=2-D_REENTRANT-D_GNU_SOURCE-D_LARGEFILE64_SOURCE-g-O2-pthread-I/usr/alibaba/install/httpd-2.2.16/include-I/usr/alibaba/install/httpd-2.2.16/include-I/usr/alibaba/install/httpd-2.2.16/include-c-omod_rewrite.lomod_rewrite.c&&touchmod_rewrite.slo

/usr/alibaba/install/httpd-2.2.16/build/libtool--silent--mode=linkgcc-omod_rewrite.la-rpath/usr/alibaba/install/httpd-2.2.16/modules-module-avoid-versionmod_rewrite.lo

4.安装动态共享对象到服务器的module目录中,并修改httpd.conf文件配置。

/usr/alibaba/install/httpd-2.2.16/bin/apxs-i-a-nrewritemod_rewrite.la

console输出如下:

/usr/alibaba/install/httpd-2.2.16/build/instdso.shSH_LIBTOOL='/usr/alibaba/install/httpd-2.2.16/build/libtool'mod_rewrite.la/usr/alibaba/install/httpd-2.2.16/modules

/usr/alibaba/install/httpd-2.2.16/build/libtool--mode=installcpmod_rewrite.la/usr/alibaba/install/httpd-2.2.16/modules/

cp.libs/mod_rewrite.so/usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.so

cp.libs/mod_rewrite.lai/usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.la

cp.libs/mod_rewrite.a/usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.a

chmod644/usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.a

ranlib/usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.a

PATH="$PATH:/sbin"ldconfig-n/usr/alibaba/install/httpd-2.2.16/modules

----------------------------------------------------------------------

Librarieshavebeeninstalledin:

/usr/alibaba/install/httpd-2.2.16/modules

Ifyoueverhappentowanttolinkagainstinstalledlibraries

inagivendirectory,LIBDIR,youmusteitheruselibtool,and

specifythefullpathnameofthelibrary,orusethe`-LLIBDIR'

flagduringlinkinganddoatleastoneofthefollowing:

-addLIBDIRtothe`LD_LIBRARY_PATH'environmentvariable

duringexecution

-addLIBDIRtothe`LD_RUN_PATH'environmentvariable

duringlinking

-usethe`-Wl,--rpath-Wl,LIBDIR'linkerflag

-haveyoursystemadministratoraddLIBDIRto`/etc/ld.so.conf'

Seeanyoperatingsystemdocumentationaboutsharedlibrariesfor

moreinformation,suchastheld(1)andld.so(8)manualpages.

----------------------------------------------------------------------

chmod755/usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.so

[activatingmodule`rewrite'in/usr/alibaba/install/httpd-2.2.16/conf/httpd.conf]

参数含义:

-i表示需要执行安装操作。

-a自动增加一个LoadModule行到httpd.conf文件中,以激活此模块,或者,如果此行已经存在,则启用之。

-n增加或启用的模块名称。

5.打开apache安装目录下的conf/httpd.conf文件,检查是否有如下行:

LoadModulerewrite_modulemodules/mod_rewrite.so

如果包含此行,则安装成功。

6.重新启动apache。

/usr/alibaba/install/httpd-2.2.16/bin/apachectlstop

/usr/alibaba/install/httpd-2.2.16/bin/apachectlstart

二、安装错误解决方法

1.现象:Can`tloacteAPImodulestaructure`mod_rewrite_module`infile

/usr/local/apache2/modules/mod_rewrite.so:/usr/local/apache2/lib/libapr-

0.so.0:undefinedsymbol:mod_rewrite_module

解决方法:httpd.conf里面写的mod_rewrite_module改成rewrite_module。

2.现象:modulerewrite_moduleisbuilt-inandcan'tbeloaded.

解决方法:表示模块是内建的,不用再手动调入,注释掉httpd.conf中如下行:

#LoadModulerewrite_modulemodules/mod_rewrite.so

检查所有内建模块的命令:apachectl-l

三、rewrite功能简介

需求:如果本地包含样式文件,则通过本地apache访问资源;否则从远端服务器获取资源。

实现:

<VirtualHost*:80>

DocumentRoot"/home/zhouyingbo/sourcecode/style_other/20111031_88209_punish_1"

ServerNamestyle.china.alibaba.com

<IfModulemod_rewrite.c>

RewriteEngineOn

RewriteCond%{DOCUMENT_ROOT}%{REQUEST_URI}!-f#不是本地文件夹

RewriteCond%{DOCUMENT_ROOT}%{REQUEST_URI}!-d#不是本地文件

RewriteRule^(.*)$http://10.20.136.137$1[L]#重写到远端服务器

</IfModule>

</VirtualHost>

说明:

RewriteEngineOn表示开启重写引擎功能;

RewriteCond语法格式为:RewriteCondTestStringCondPattern,当TestString满足CondPattern时,执行RewriteRule定义的规则。

RewriteRule语法格式为:RewriteRulePatternSubstitution,Pattern为posix正则式,Substitution用来替换相应的匹配。

四、参考资料

RewriteCond参考文档:http://blog.csdn.net/keyunq/article/details/2536875

RewriteRule参考文档:http://blog.csdn.net/bjbs_270/article/details/2097064

相关推荐