安装好discuz后,在discuz管理界面设置了伪静态,并在apache的配置文件中添加了规则。但是始终不好用,刚才终于恍然大悟,没有rewrite模块啊!

工作中要用到伪静态功能,所以说rewrite模块是必不可少的,但在本机上已经安装了apache,没有安装rewrite模块,下面简单记录一下鄙人安装rewrite模块的步骤:

环境:

[root@web-B ~]# cat /etc/redhat-release
CentOS release 6.4 (Final)
[root@web-B ~]# uname -r
2.6.32-358.el6.x86_64
[root@web-B ~]# uname -m
x86_64
[root@web-B ~]#
[root@web-B ~]# apachectl -v
Server version: Apache/2.4.6 (Unix)
Server built:   Oct 21 2013 11:52:02

下面开始rewrite模块的安装:

1)apache源码包中找到mod_rewrite.c文件,在源码包的module/mappers下,进入所在目录

[root@web-B ~]# cd /software/lamp/httpd-2.4.6/modules/mappers/

2) 将mod_rewrite.c程序编译成共享模块,以便在运行时加载到apache服务器中。/application/apache/为本机apache安装目录

[root@web-B mappers]# /application/apache/bin/apxs -c mod_rewrite.c
/application/apr/build-1/libtool --silent --mode=compile gcc -std=gnu99 -prefer-pic   -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/application/apache2.4.6/include  -I/application/apr/include/apr-1   -I/application/apr-util/include/apr-1   -c -o mod_rewrite.lo mod_rewrite.c && touch mod_rewrite.slo
/application/apr/build-1/libtool --silent --mode=link gcc -std=gnu99    -o mod_rewrite.la  -rpath /application/apache2.4.6/modules -module -avoid-version    mod_rewrite.lo
[root@web-B mappers]#

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

[root@web-B mappers]# /application/apache/bin/apxs -i -a -n rewrite mod_rewrite.la
/application/apache2.4.6/build/instdso.sh SH_LIBTOOL='/application/apr/build-1/libtool' mod_rewrite.la /application/apache2.4.6/modules
/application/apr/build-1/libtool --mode=install install mod_rewrite.la /application/apache2.4.6/modules/
libtool: install: install .libs/mod_rewrite.so /application/apache2.4.6/modules/mod_rewrite.so
libtool: install: install .libs/mod_rewrite.lai /application/apache2.4.6/modules/mod_rewrite.la
libtool: install: install .libs/mod_rewrite.a /application/apache2.4.6/modules/mod_rewrite.a
libtool: install: chmod 644 /application/apache2.4.6/modules/mod_rewrite.a
libtool: install: ranlib /application/apache2.4.6/modules/mod_rewrite.a
libtool: finish: PATH="/application/mysql/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/sbin" ldconfig -n /application/apache2.4.6/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /application/apache2.4.6/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /application/apache2.4.6/modules/mod_rewrite.so
[activating module `rewrite' in /application/apache2.4.6/conf/httpd.conf]

参数含义:
-i 表示需要执行安装操作。
-a 自动增加一个LoadModule行到httpd.conf文件中,以激活此模块,或者,如果此行已经存在,则启用之。
-n 增加或启用的模块名称。

4)查看是否安装成功,httpd.conf中有LoadModule rewrite_module modules/mod_rewrite.so行,即表示安装成功。

[root@web-B mappers]# grep rewrite_module /application/apache/conf/httpd.conf
LoadModule rewrite_module     modules/mod_rewrite.so
[root@web-B mappers]#

5)重新生效apache所有配置

[root@web-B mappers]# apachectl graceful

discuz伪静态配置

1)登录discuz管理中心,全局-SEO优化-URL静态化,将下面需要静态化的再后面打勾。点击rewrite规则,复制对应的规则到服务器。

2)将复制好的规则,粘贴到httpd.conf

<Directory "/data0/www/bbs">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
<IfModule mod_rewrite.c>  ###复制伪静态规则的内容,放在下面即可
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^blog-([0-9]+)-([0-9]+)\.html$ home.php?mod=space&uid=$1&do=blog&id=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ plugin.php?id=$1:$2&%1
</IfModule> ###结束
</Directory>
<Directory "/data0/www/blog">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>