我正在使用主 index.php 文件来处理大部分请求,所以我的基本 .htacess 看起来像 this one :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L]
但我有一些请求(如/signin 或/signout)需要直接从同名的 .php 文件(/signin.php 和/signout.php)处理。
所以我的 .htaccess 规则需要是:“if %{REQUEST_FILENAME}.php
does not exists redirect to /index.php?page=$1
else redirect to %{REQUEST_FILENAME}.php
”。
我该怎么做?
最佳答案
用这个替换你的代码:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
这会将不是有效文件或目录且在您的文件系统中不存在匹配的 .php
的所有内容发送到 /index.php
。
关于php - htaccess 重定向隐藏 php 扩展或传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223226/