实例说明Php伪静态 .htaccess Rewrite apache nginx 的设置

PHP 林涛 9374℃ 0评论

设置伪静态首先要确定服务器是否支持Rewrite,然后写入重写规则。

这里以www.58ningjin.com 为例进行说明。

操作系统:centos, 服务器环境:wdcp管理系统(开源的),引擎:apache+nginx;

在wdcp管理系统中,如果使用apache+nginx引擎,则需要修改apache配置,这一点非常重要;下面介绍一下怎样修改apache配置。

1.检查系统是否支持Rewrite:

·命令:/www/wdlinux/apache/bin/httpd -l     如果存在 mod_rewrite.c ,这说明支持Rewrite;

·phpinfo ,在“Loaded Modules”中如果包括“mod_rewrite”,这说明支持Rewrite;

如果没有开启“mod_rewrite”,则打开目录 您的apache安装目录“/apache/conf/” 下的 httpd.conf 文件,通过Ctrl+F查找到“LoadModule rewrite_module”,将前面的”#”号删除即可。如果没有查找到,则到“LoadModule” 区域,在最后一行加入“LoadModule rewrite_module modules/mod_rewrite.so”(必选独占一行),然后重启apache服务器即可。

 

2.修改apache配置:

找到www.58ningjin.com的配置文件,做如下修改(红色字体为规则部分):

<VirtualHost *:88>
DocumentRoot /www/web/58ningjin/public_html
ServerName 58ningjin.com
ServerAlias www.58ningjin.com 58ningjin.com
ErrorDocument 400 /errpage/400.html
ErrorDocument 403 /errpage/403.html
ErrorDocument 404 /errpage/404.html
php_admin_value open_basedir /www/web/58ningjin:/tmp

RewriteEngine On
       RewriteRule ^(.*)/(list|bencandy)-([-_0-9a-zA-Z]+)\.([a-zA-Z0-9]+)$ $1/$2\.php\?stringID=$3
       RewriteRule ^(.*)/([A-Z])([^\/]+)/([^\/]+)/f([^\/]+)\.([a-zA-Z0-9]+)$ $1/$2$3/bencandy\.php\?Fid=$4&Id=$5
       RewriteRule ^(.*)/([A-Z])([^\/]+)/([^\/]+)/([^\/]+)/$ $1/$2$3/list\.php\?Fid=$5&zone_street=$4
       RewriteRule ^(.*)/([A-Z])([^\/]+)/([^\/]+)/$ $1/$2$3/list\.php\?Fid=$4

<IfModule mod_deflate.c>
DeflateCompressionLevel 7
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE css js html htm gif jpg png bmp php
</IfModule>
</VirtualHost>
<Directory /www/web/58ningjin>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

修改后重新启动服务器就可,这个方法是写在网站配置文件上的,如果用.htaccess文件大同小异,只要理解了原理就可以了。

需要注意的是:

1.apache和nginx的重写规则写法是不一样的。

2.RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2

([a-zA-Z]{1,})-([0-9]{1,}).html$是规则,index.php?action=$1&id=$2是要替换的格式,$1代表第一个括号匹配的值,$2代表第二个,如此类推!!

如需转载请注明: 转载自26点的博客

本文链接地址: 实例说明Php伪静态 .htaccess Rewrite apache nginx 的设置

转载请注明:26点的博客 » 实例说明Php伪静态 .htaccess Rewrite apache nginx 的设置

喜欢 (0)
发表我的评论
取消评论

表情
(2)个小伙伴在吐槽
  1. 很详细,谢谢分享
    冶金行业报告2013-06-21 11:45 回复
  2. 师傅说,看了不回,木有jj。马克下文章,慢慢看;忠实粉丝,期待更新。我是一鸣,回访一下吧。。。
    产业报告调研2013-05-17 14:40 回复