PHP伪静态配置教程(附文件下载)

Joson 2021-07-09 14:27:20 1688 抢沙发

根据服务器环境自行配置伪静态规则:

Apache伪静态(即系统自带的.htaccess文件):

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]
</IfModule>

Nginx伪静态:

location / {
	#//...省略部分代码
	if (!-e $request_filename){
		rewrite  ^(.*)$  /index.php?s=$1  last;   break;
	}
}

如果你的应用安装在二级目录,Nginx的伪静态方法设置如下,其中youdomain是所在的目录名称。

location /youdomain/ {
    if (!-e $request_filename){
        rewrite  ^/youdomain/(.*)$  /youdomain/index.php?s=$1  last;
    }
}

举个栗子:


如果你用的是本机电脑上的phpstudy环境的话,打开配置文件( nginx/conf/vhost.conf ):

location / {
	index  index.html index.htm index.php;
	#autoindex  on;
	if (!-e $request_filename) {
		rewrite ^(.*)$ /index.php?s=$1 last; break;
	}			
}

90%的情况下,Nginx的以上配置是完全没问题的,如果你用的是老古董的话,那么你可以尝试修改配置文件:


“common/config/config.php”,修改 配置项 “set_pathinfo” 为 true 来实现!

IIS伪静态:

如果你的服务器环境支持ISAPI_Rewrite的话,可以配置httpd.ini文件,添加下面的内容:

RewriteRule (.*)$ /index\.php\?s=$1 [I]

在IIS的高版本下面可以配置web.config,在中间添加rewrite节点:

<rewrite>
 <rules>
 <rule name="OrgPage" stopProcessing="true">
 <match url="^(.*)$" />
 <conditions logicalGrouping="MatchAll">
 <add input="{HTTP_HOST}" pattern="^(.*)$" />
 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
 </conditions>
 <action type="Rewrite" url="index.php?s={R:1}" />
 </rule>
 </rules>
</rewrite>

文件下载:

PHP伪静态配置文件.zip


文章版权及转载声明

作者:Joson ,本文地址:https://www.sojoson.com/mbjc/35.html发布于 2021-07-09 14:27:20
文章转载或复制请以超链接形式并注明出自SoJoson博客

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论

快捷回复:

评论列表 (有4条评论,1688人围观) 参与讨论