如何利用.htaccess文件对PHP网站或文件进行伪静态处理

如何利用.htaccess文件对PHP网站或文件进行伪静态处理,第1张

利用.htaccess文件对PHP网站进行伪静态处理要考虑两点:

一.服务器支持伪静态,比如Apache要开启mod_rewrite模块支持

二.利用.htaccess完成伪静态需要根据不同的网站系统进行不同的设置,以下是一些常见系统的.htaccess设定:

01.wordpress:

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

02.Phpwind

RewriteEngine On

RewriteBase /

RewriteRule ^(.*)-htm-(.*)$ $1.php?$2

RewriteRule ^(.*)simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2

03.Discuz

RewriteEngine On

# Rewrite 系统规则请勿修改

RewriteRule ^archiver/((fid|tid)-[0-9]+\.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

04.ecshop

RewriteEngine On

# direct one-word access

RewriteRule ^index\.html$ index\.php [L]

RewriteRule ^category$ index\.php [L]

# access any object by its numeric identifier

RewriteRule ^feed-c([0-9]+)\.xml$ feed\.php\?cat=$1 [L]

RewriteRule ^feed-b([0-9]+)\.xml$ feed\.php\?brand=$1 [L]

RewriteRule ^feed\.xml$ feed\.php [L]

RewriteRule ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ category\.php\?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 [QSA,L]

RewriteRule ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$ category\.php\?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 [QSA,L]

RewriteRule ^category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ category\.php\?id=$1&brand=$2&page=$3&sort=$4&order=$5 [QSA,L]

RewriteRule ^category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$ category\.php\?id=$1&brand=$2&page=$3 [QSA,L]

RewriteRule ^category-([0-9]+)-b([0-9]+)(.*)\.html$ category\.php\?id=$1&brand=$2 [QSA,L]

RewriteRule ^category-([0-9]+)(.*)\.html$ category\.php\?id=$1 [QSA,L]

RewriteRule ^goods-([0-9]+)(.*)\.html$ goods\.php\?id=$1 [QSA,L]

RewriteRule ^article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ article_cat\.php\?id=$1&page=$2&sort=$3&order=$4 [QSA,L]

RewriteRule ^article_cat-([0-9]+)-([0-9]+)(.*)\.html$ article_cat\.php\?id=$1&page=$2 [QSA,L]

RewriteRule ^article_cat-([0-9]+)(.*)\.html$ article_cat\.php\?id=$1 [QSA,L]

RewriteRule ^article-([0-9]+)(.*)\.html$ article\.php\?id=$1 [QSA,L]

RewriteRule ^brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html brand\.php\?id=$1&cat=$2&page=$3&sort=$4&order=$5 [QSA,L]

RewriteRule ^brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html brand\.php\?id=$1&cat=$2&page=$3 [QSA,L]

RewriteRule ^brand-([0-9]+)-c([0-9]+)(.*)\.html brand\.php\?id=$1&cat=$2 [QSA,L]

RewriteRule ^brand-([0-9]+)(.*)\.html brand\.php\?id=$1 [QSA,L]

RewriteRule ^tag-(.*)\.html search\.php\?keywords=$1 [QSA,L]

RewriteRule ^snatch-([0-9]+)\.html$ snatch\.php\?id=$1 [QSA,L]

RewriteRule ^group_buy-([0-9]+)\.html$ group_buy\.php\?act=view&id=$1 [QSA,L]

RewriteRule ^auction-([0-9]+)\.html$ auction\.php\?act=view&id=$1 [QSA,L]

05.phpcms

RewriteEngine On

RewriteRule ^(.*)content-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/index\.php\?m=content&c=index&a=show&catid=$2&id=

$3&page=$4

RewriteRule ^(.*)show-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/index\.php\?m=content&c=index&a=show&catid=$2&id=

$3&page=$4

RewriteRule ^(.*)list-([0-9]+)-([0-9]+).html$ $1/index\.php\?m=content&c=index&a=lists&catid=$2&page=$3

首先,apache配置下,有一个配置表示默认将后缀为.php的文件进行解析,当然你可以进行配置,改成其它后缀的。不过你既然有.htaccess文件了,为什么要加那个后缀呢?一般里面的规则如下

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]

</IfModule>

定义好这个规则后就不用加text.php了。只不过,apache一般默认解析的是index.php,所以我提供的规则也是默认指向index.php的。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11574010.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存