PHP-添加尾随斜杠(PHP/)会导致页面显示错误


PHP - Adding a Trailing Slash (.php/) Results in the Page displaying with Errors

我的网站目前遇到了一个问题。如果有人输入http://mysite.com/index.php,它运行良好,并显示index.php。但是,如果有人输入http://mysite.com/index.php/,index.php丢失了大部分图像,现在充当了一个文件夹,尽管它不是。这发生在我所有的.php文件上。

然而,在其他网站上,这个问题并没有发生。我一直在考虑使用这个htaccess代码:

# Remove garbage info after .php
RewriteRule ^([^.]+'.php)[/.] http://www.example.com/$1 [R=301,L]

我的问题基本上是。。。这个问题的最佳解决方案是什么?此外,如果我使用上面提到的htaccess RewriteRule,页面会像/test.php吗?id=5被精简为/test.php(基本上删除了.php之后的所有内容)?

谢谢,
标记

处理此问题的最佳方法是为您的站点创建一个单一入口点。

所以,在你的.htaccess中,你可以写:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

现在,您将始终在index.php中输入,从那里您可以对url执行您喜欢的操作:

$url = rtrim( $url, '/' );
$request = explode( '/', $url );

看看我的教程/博客。