PHP:htaccess:重写规则在不存在的页面上出现 500 错误
我有一个网站,它基本上使用 RewriteRule 隐藏 .php,并且还允许脚本通过斜杠确定参数。
所以例子: http://www.dealhandler.com/deals/san-jose/Living %20Social/233102
该脚本是 Deals.php,它接受 san-jose、living Social 和 deal id 等参数。
这是我的 .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^dealhandler.com
RewriteRule ^(.*)$ http://www.dealhandler.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(.*)$ $1.php$2 [L,QSA]
问题: 如果访问者输入根级别上没有脚本的无效网址,我会收到 500 错误。
示例:http://www.dealhandler.com/idontexist 我会得到:
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.
是否有一种方法可以保留当前功能,但足够聪明,可以在根级别文件不存在时停止并重定向?
I have a website that basically hides the .php using RewriteRule and also allow the script to determine the parameter by slashes.
So example:
http://www.dealhandler.com/deals/san-jose/Living%20Social/233102
The script is deals.php which takes in parameters like san-jose, living social, and the deal id.
Here's my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^dealhandler.com
RewriteRule ^(.*)$ http://www.dealhandler.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(.*)$ $1.php$2 [L,QSA]
PROBLEM:
If a visitor enters an invalid url that doesn't have a script on the root level, i get 500 ERROR.
Example: http://www.dealhandler.com/idontexist
I'll get:
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.
Is there a way to keep the current function but be smart enough to stop and redirect if the root level file doesn't exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
乍一看,只要找不到具有给定名称的文件,您就会无限地添加 .php 。如果您打开调试日志记录,您应该能够轻松确认这一点。
您最好的选择是
1) 仅将 .php 附加到尚未以 .php 结尾的字符串,因此只会发生一次
或者
2) 在应用规则之前检查另一个条件中是否存在潜在的新 .php 文件名
At first glance, it appears like you're infinitely adding .php whenever it doesn't find a file with the given name. If you turn on debug logging, you should be able to confirm this easily.
Your best options, are to either
1) Only append .php to strings that don't already end in .php, so it will only happen once
or
2) Check if the potential new .php filename exists in another Condition before applying the Rule at all
尝试在 RewriteRule 之前添加此内容:
未映射到有效 php 文件(如 idontexist.php)的请求应该只返回 404。
Try adding this right before your RewriteRule:
The requests that don't map to a valid php file (like idontexist.php) should just return a 404.
这里有一些应该避免无限循环的事情:只要首先测试它是否不是文件或目录,如果以“php”结尾,则立即停止。如果不是,请继续执行规则并尝试自己的测试:
Here's something that should avoid infinite loops: just test first if it's not a file or a dir, and if ends with "php" then stop immediately. If not the rules carry on and try your own test:
检查您的 ../apache/conf/httpd.conf 并查找:
如果井号 (#) 作为第一个字母出现,请将其删除,保存并重新启动服务器。
Check your ../apache/conf/httpd.conf and look for:
If the hash sign (#) appears as the first letter, remove it, save it and restart server.