Sunil Kumar
Sunil Kumar

Reputation: 1391

mod rewrite back reference not working

I am working on rewrite but it is not working for me. I already wasted my 2 days for it. Your help will be appreciatable.

RewriteRule test/(.*)/(.*)/$ /include/test?$1=$2   //not working
RewriteRule test/(\w+)/(\w+)/$ /include/test?$1=$2   //not working
RewriteRule ^test/(\w+)/(\w+)/$ /include/test?$1=$2  //not working

RewriteRule .* include/test.php?id=123     //working

Trying PHP code

echo $_SERVER['PHP_SELF']   // include/test.php/id/1234 (after rewrite)
                            // expecting :  /include/test.php?id=1234

Request by user url :

 http://www.example.com/include/test/id/1234

Rewite to:

http://www.example.com/include/test.php?id=1234

Possible issues:

Main problem:

Not getting $_GET value

Test.php

<?php
ini_set("display_errors",1);
echo $_GET['id'];
echo  $_SERVER['PHP_SELF'].'<br/>';
echo $_SERVER['PATH_INFO'];
?>

On requesting

http://www.example.com//include/test/id/1234

Output:

/include/test.php/id/1234
/id/1234

Upvotes: 0

Views: 136

Answers (1)

vee
vee

Reputation: 38645

The rules look okay, the only problem I can see is the trailing slash for your test URI: http://www.example.com/include/test/id/1234

Please try:

RewriteRule test/(.*)/(.*)/?$ /include/test.php?$1=$2
RewriteRule test/(\w+)/(\w+)/?$ /include/test.php?$1=$2
RewriteRule ^test/(\w+)/(\w+)/?$ /include/test.php?$1=$2

Also you do not need all three of the rules above. The first one should cover both first and second. So, you can remove the second one. I just kept it there assuming these are your different tests.

Update:

Adds .php extension to destination URI, i.e. replaces /include/test?$1=$2 with /include/test.php?$1=$2

Upvotes: 1

Related Questions