PHP:htaccess:重写规则在不存在的页面上出现 500 错误

发布于 2024-12-19 02:33:51 字数 1020 浏览 0 评论 0原文

我有一个网站,它基本上使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把昨日还给我 2024-12-26 02:33:51

乍一看,只要找不到具有给定名称的文件,您就会无限地添加 .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

单身情人 2024-12-26 02:33:51

尝试在 RewriteRule 之前添加此内容:

RewriteCond %{REQUEST_URI} !^/[^/]+\.php

未映射到有效 php 文件(如 idontexist.php)的请求应该只返回 404。

Try adding this right before your RewriteRule:

RewriteCond %{REQUEST_URI} !^/[^/]+\.php

The requests that don't map to a valid php file (like idontexist.php) should just return a 404.

懒猫 2024-12-26 02:33:51

这里有一些应该避免无限循环的事情:只要首先测试它是否不是文件或目录,如果以“php”结尾,则立即停止。如果不是,请继续执行规则并尝试自己的测试:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^dealhandler.com 
RewriteRule ^(.*)$ http://www.dealhandler.com/$1 [R=301,L]

# if it's not a file...
RewriteCond %{REQUEST_FILENAME} !-f
# ... and it's not a dir...
RewriteCond %{REQUEST_FILENAME} !-d
# ...and it ends by "php" then stop:
RewriteRule (.*)\.php$ - [L]

# if it's not a file...
RewriteCond %{REQUEST_FILENAME} !-f
# ... and it's not a dir...
RewriteCond %{REQUEST_FILENAME} !-d
# ...rewrite it to php:
RewriteRule ^([^/]*)(.*)$ $1.php$2 [L,QSA]

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:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^dealhandler.com 
RewriteRule ^(.*)$ http://www.dealhandler.com/$1 [R=301,L]

# if it's not a file...
RewriteCond %{REQUEST_FILENAME} !-f
# ... and it's not a dir...
RewriteCond %{REQUEST_FILENAME} !-d
# ...and it ends by "php" then stop:
RewriteRule (.*)\.php$ - [L]

# if it's not a file...
RewriteCond %{REQUEST_FILENAME} !-f
# ... and it's not a dir...
RewriteCond %{REQUEST_FILENAME} !-d
# ...rewrite it to php:
RewriteRule ^([^/]*)(.*)$ $1.php$2 [L,QSA]
何其悲哀 2024-12-26 02:33:51

检查您的 ../apache/conf/httpd.conf 并查找:

#LoadModule rewrite_module modules/mod_rewrite.so

如果井号 (#) 作为第一个字母出现,请将其删除,保存并重新启动服务器。

Check your ../apache/conf/httpd.conf and look for:

#LoadModule rewrite_module modules/mod_rewrite.so

If the hash sign (#) appears as the first letter, remove it, save it and restart server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文