我正在更新一个 php 应用程序,该应用程序当前不使用 url 重写。目的是隐藏文件扩展名。网站总体结构如下
root/
index.php
login.php
page1.php
page2.php
page3.php
page4.php
page5.php
page6.php
page7.php
page8.php
subfolder/
index.php
validpage.php
images/
css/
上述结构中的子文件夹的独特之处在于它是唯一包含 php 文件的子文件夹。所有其他文件都位于根文件夹中。
我经历过这些问题Mod 重写和 PHP https://stackoverflow.com/questions/788188/mod-rewrite-and-php and Mod-Rewrite 还是 PHP 路由器? https://stackoverflow.com/questions/1868154/mod-rewrite-or-php-router and mod_rewrite、php 和 .htaccess 文件 https://stackoverflow.com/questions/265898/mod-rewrite-php-and-the-htaccess-file
Updated.htaccess。谢谢@托马斯马尔特 https://stackoverflow.com/users/438024/thomasmalt
RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l
RewriteRule ^.*$ - [NC,L]
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
# add .php to the url
RewriteRule ^(.*)$ $1.php
现在的问题是,当我尝试访问 localhost/inex.php 等不存在的页面时,我收到 500 而不是 404 错误。这阿帕奇错误日志给我以下错误。 (我访问了 localhost/index ,它加载了 index.php 的内容,然后加载了 localhost/inex ,这给了我一个 500 错误。)
[Sat Sep 25 14:44:26 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/index
[Sat Sep 25 14:44:36 2010] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Update: 找到了我问题的答案here https://stackoverflow.com/questions/1745680/htaccess-rewrite-causes-500-error-instead-of-404/1750696#1750696。问题解决了。