检查并加载mod_rewrite模块
首先需要确认当前运行的 Apache 具体版本,并核实系统是否已加载 mod_rewrite 模块。
若使用的是 Apache 1.x 版本,请检查 conf/httpd.conf 配置文件内是否包含以下两行指令:
LoadModule rewrite_module libexec/mod_rewrite.so AddModule mod_rewrite.c
若使用的是 Apache 2.x 版本,请检查 conf/httpd.conf 配置文件内是否包含如下单行指令: LoadModule rewrite_module modules/mod_rewrite.so
若上述模块配置存在,需在配置文件(httpd.conf)中添加相关设置。特别需要注意的是,如果网站是通过虚拟主机定义的,务必将配置写入对应的 <VirtualHost> 标签内部;若置于虚拟主机配置之外,可能导致功能失效。修改完毕后,请重启 Apache 服务以生效。
配置.htaccess实现URL重写
在确认 mod_rewrite 模块已开启后,针对使用虚拟主机的用户,还可通过创建 .htaccess 文件来实现 URL 重写。首先需要在目标站点目录的配置中启用 AllowOverride All(默认状态通常为 AllowOverride None)。
.htaccess 文件的配置内容示例如下: # 启用 RewriteEngine 模式 RewriteEngine On # 将 /dir 替换为您的实际站点目录路径,若程序位于根目录,则改为 / RewriteBase /dir # 根据具体系统需求修改下方的 Rewrite 规则
RewriteRule ^archiver/((fid|tid)-[\w\-]+\.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 ^space-(username|uid)-(.+)\.html$ space.php?$1=$2 RewriteRule ^tag-(.+)\.html$ tag.php?name=$1 将编辑好的 .htaccess 文件放置于对应目录下,即可支持 URL 静态地址格式,从而便于搜索引擎进行抓取。