简体   繁体   中英

URL rewriting in .htaccess is causing HTTP request failed error

I am working on a php project where i am using .htaccess mod_rewrite to generate friendly urls for the website. I am working on local machine. Here are the urls,

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

I want these urls to look like following

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

I wrote the following code in my .htaccess file.

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

Now at this time every thing is working good. It is producing the http://example.com/sports/cricket/ball/ url. But when write rewrite rule for http://example.com/sports/cricket/ it is giving error. I am using this rewrite rule (same as previous url).

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

This line is making apache very slow and at last it gives following error.

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

I changed rewrite rule as RewriteRule ([^/.]+)/ list.php?Cat=$1 [L] . Now it's giving same error but this time it is recognizing the query string parameter.

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

I don't know where i am wrong in writing this rule.

Extend your rewrite parameters: check if specified Url doesn't refer to existing file(directory) on your local server

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM