php - URL rewriting in .htaccess is causing HTTP request failed error

493

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 thehttp://example.com/sports/cricket/ball/ url. But when write rewrite rule forhttp://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 asRewriteRule ([^/.]+)/ 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.

269

Answer

Solution:

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

People are also looking for solutions to the problem: Autologin with User and Password in URL when opening a php site

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.