首页 > 开发 > Apache > 正文

apache 伪静态配置方法

2020-10-12 18:50:54
字体:
来源:转载
供稿:网友

Apache伪静态html(URL Rewrite)设置法

一 打开 Apache 的配置文件 httpd.conf 。

二 将#LoadModule rewrite_module modules/mod_rewrite前面的#去掉

三 在 httpd.conf中添加:

  1. <IfModule mod_rewrite.c> 
  2.     RewriteEngine On 
  3.     #RewriteCond %{ENV:SCRIPT_URL} (?:index|dispbbs)[-0-9]+.html 
  4.     RewriteRule ^(.*?(?:index|dispbbs))-([-0-9]+).html$ $1.php? 
  5. __is_apache_rewrite=1&__rewrite_arg=$2 
  6. </IfModule> 
  7. <ifmodule mod_rewrite.c> 
  8.  RewriteEngine On 
  9.  RewriteRule /news/top2007,(d+).html$   /news/readmore.php?id=$1 
  10. </ifmodule> 

四 保存httpd.conf并重启apache。

Apache Web Server(独立主机用户)

首先确定您使用的 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

如果存在,那么在配置文件(通常就是 conf/httpd.conf)中加入如下代码。此时请务必注意,如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即 <VirtualHost>中去,如果加在虚拟主机配置外部将可能无法使用。改好后然后将 Apache 重启。

  1. <IfModule mod_rewrite.c>  
  2. RewriteEngine On  
  3. RewriteRule ^(.*)/ivideo(-tv-([0-9]+))?(-ti-([0-9]+))?(-tc-([0-9]+))?(-page- 
  4. ([0-9]+))?.html$ $1/ivideo.php?tv=$3&ti=$5&tc=$7&page=$9  
  5. RewriteRule ^(.*)/ispecial(-tv-([0-9]+))?(-ti-([0-9]+))?(-tc-([0-9]+))?(-page- 
  6. ([0-9]+))?.html$ $1/ispecial.php?tv=$3&ti=$5&tc=$7&page=$9  
  7. RewriteRule ^(.*)/icategory.html$ $1/icategory.php  
  8. RewriteRule ^(.*)/category-cid-([0-9]+)(-tag-([^-]*))?(-timelimit-([0-9]+))?(- 
  9. orderlimit-([0-9]+))?(-page-([0-9]+))?.html$ $1/category.php? 
  10. cid=$2&tag=$4&timelimit=$6&orderlimit=$8&page=$10  
  11. RewriteRule ^(.*)/vspace-(mid|username)-(.+).html$ $1/vspace.php?$2=$3  
  12. RewriteRule ^(.*)/video-(vid|ivid)-(.+).html$ $1/video.php?$2=$3  
  13. RewriteRule ^(.*)/special-spid-([0-9]+).html$ $1/special.php?spid=$2  
  14. </IfModule>  

如果没有安装 mod_rewrite,您可以重新编译 Apache,并在原有 configure 的内容中加入 --enable-rewrite=shared,然后再在 Apache 配置文件中加入上述代码即可。

Apache Web Server(虚拟主机用户)

在开始以下设置之前,请首先咨询您的空间服务商,空间是否支持 Rewrite 以及是否支持对站点目录中 .htaccess 的文件解析,否则即便按照下面的方法设置好了,也无法使用。

检查播客所在目录中是否存在 .htaccess 文件,如果不存在,请手工建立此文件。Win32系统下,无法直接建立 .htaccess 文件,您可以从其他系统中拷贝一份,或者在discuz.net 技术支持栏目中下载此文件。编辑并修改 .htaccess 文件,添加以下内容:

  1. #将 RewriteEngine 模式打开 RewriteEngine On  
  2. # 修改以下语句中的 /supev 为你的播客目录地址,如果程序放在根目录中,请将 /supev 修改为 /  
  3. ​RewriteBase /supev  
  4.  
  5. # Rewrite 系统规则请勿修改 RewriteRule 
  6.  
  7. RewriteRule ^ivideo(-tv-([0-9]+))?(-ti-([0-9]+))?(-tc-([0-9]+))?(-page-([0-9] 
  8. +))?.html$ ivideo.php?tv=$2&ti=$4&tc=$6&page=$8  
  9. RewriteRule ^ispecial(-tv-([0-9]+))?(-ti-([0-9]+))?(-tc-([0-9]+))?(-page-([0- 
  10. 9]+))?.html$ ispecial.php?tv=$2&ti=$4&tc=$6&page=$8  
  11. RewriteRule ^icategory.html$ icategory.php  
  12. RewriteRule ^category-cid-([0-9]+)(-tag-([^-]*))?(-timelimit-([0-9]+))?(- 
  13. orderlimit-([0-9]+))?(-page-([0-9]+))?.html$ category.php? 
  14. cid=$1&tag=$3&timelimit=$5&orderlimit=$7&page=$9  
  15. RewriteRule ^vspace-(mid|username)-(.+).html$ vspace.php?$1=$2  
  16. RewriteRule ^video-(vid|ivid)-(.+).html$ video.php?$1=$2  
  17. RewriteRule ^special-spid-([0-9]+).html$ special.php?spid=$1 
  18.  
  19. <VirtualHost *:80> 
  20. DocumentRoot /home/www_php168 
  21. ServerName www.111cn.cn 
  22. <IfModule mod_rewrite.c> 
  23. RewriteEngine On 
  24. RewriteRule ^(.*)/list-([0-9]+)-([0-9]+).htm$ $1/list.php?fid=$2&page=$3 
  25. RewriteRule ^(.*)/bencandy-([0-9]+)-([0-9]+)-([0-9]+).htm$ $1/bencandy.php? 
  26. fid=$2&id=$3&page=$4 
  27. </IfModule> 
  28. </VirtualHost> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表