【问题标题】:URL rewriting in .htaccess is causing HTTP request failed error.htaccess 中的 URL 重写导致 HTTP 请求失败错误
【发布时间】:2015-12-28 17:52:06
【问题描述】:

我正在开发一个 php 项目,我正在使用 .htaccess mod_rewrite 为网站生成友好的 url。我正在本地机器上工作。这是网址,

http://localhost/sports/detail.php?Cat=cricket&Pro=ball
http://localhost/sports/list.php?Cat=cricket

我希望这些网址如下所示

http://localhost/sports/cricket/ball/
http://localhost/sports/cricket/

我在 .htaccess 文件中编写了以下代码。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ([^/.]+)/([^/.]+)/?$ detail.php?Cat=$1&Pro=$2 [L]
</IfModule>

现在,一切都运行良好。它正在生成http://example.com/sports/cricket/ball/ url。但是当为http://example.com/sports/cricket/ 编写重写规则时,它给出了错误。我正在使用这个重写规则(与之前的 url 相同)。

RewriteRule ([^/.]+)/?$ list.php?Cat=$1 [L]

这一行使 apache 变得非常慢,最后它给出了以下错误。

Warning: file_get_contents(http://localhost/Sports/api/v1/list_one.php?Cat=php): failed to open stream: HTTP request failed! in C:\xampp\htdocs\Sports\list.php on line 5

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\Sports\list.php on line 5

我将重写规则更改为RewriteRule ([^/.]+)/ list.php?Cat=$1 [L]。现在它给出了同样的错误,但这次它正在识别查询字符串参数。

Warning: file_get_contents(http://localhost/Sports/api/v1/list_one.php?Cat=cricket): failed to open stream: HTTP request failed! in C:\xampp\htdocs\Sports\list.php on line 5

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\Sports\list.php on line 5

我不知道我写这个规则哪里错了。

【问题讨论】:

  • 您需要从 PHP 端调试它,以确定意外输入是什么。

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

扩展您的rewrite 参数:检查指定的Url 是否不引用本地服务器上的现有文件(目录)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index\.php$ - [L]
RewriteRule ([^/]+)/([^/]+)/?$ detail.php?Cat=$1&Pro=$2 [L]
</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2022-10-02
    相关资源
    最近更新 更多