伪静态

php处理伪静态:

1.path_info模式

通过正则表达式去分析伪静态url地址

注意:nginx默认不支持path_info模式

如:url="http://test.com/list.php?type=2&cid=1"

可以转换为"http://test.com/list/2/1.html"

一般使用preg_match(),去匹配/2/1.html,然后根据这个值去组装成url地址的样子

2.web服务器rewrite配置

 

真实访问的url:http://test.com/detail.php?id=10

apache下rewrite配置:

LoadModule rewrite_module modules/mod_rewrite.so

RewriteRule ^/detail/([0-9]*).html$ /detail.php?id=$1

当服务器下有detail/10.html这个文件时,

输入那个伪静态地址想要访问这个真实的静态文件怎么办呢?

配置Apache:

RerwriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d 

(服务器下如果没有没有请求的目录,则继续执行下面的配置)

RerwriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f 

(服务器下如果没有没有请求的文件,则继续执行下面的配置)

所以最后的配置是

RerwriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d

RerwriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f

RewriteRule ^/detail/([0-9]*).html$ /detail.php?id=$1

nginx下rewrite配置:

rewrite ^/detail/([0-9]*).html$ /detail.php?id=$1 last;

  

原文地址:https://www.cnblogs.com/lauhp/p/7999534.html