如何利用.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

设置伪静态规则的具体步骤如下:

以IIS10 下设置伪静态规则为例:

1、首先需要安装Url重写模块,百度搜索,下载完安装,URL Rewrite Module。

2、然后重启机器,可以在iis全局看到URL rewrite模块。

3、然后点击要设置伪静态规则的站点,双击“URL 重写”图标。

4、然后进入“URL 重写”界面,点击右侧的“导入规则”。

5、进入“导入 mod_rewrite”界面,点击配置文件下面框最后的三个点。

6、找到伪静态规则文件:.htaccess,选中并点击打开。

7、回到“导入 mod_rewrite”界面,点击“导入”规则。

8、最后,点击右栏的“应用”按钮。

9、至此伪静态配置完成,iis上的伪静态规则是写在web.confg,在网站根目录上web.confg已写上相关程序规则


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/471857.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存