htAccess 重定向隐藏 PHP 扩展或传递参数


htaccess redirect hiding the php extension or pass a parameter

我正在使用main index.php文件来处理大多数请求,所以我的基本.htacess看起来像这样:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L]

但是我有一些请求(如/signin 或/signout),需要直接从具有相同名称(/signin.php 和/signout.php).php文件中处理。

所以我的.htaccess规则需要:"if %{REQUEST_FILENAME}.php不存在重定向到/index.php?page=$1否则重定向到%{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,只需重定向索引.php上的每个调用。在那里,您可以读取URL中给出的所有参数。

$aParams = explode("/", $_SERVER["REQUEST_URI"]);

现在,您可以根据需要处理所有参数并重定向。