Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php .htaccess url rewrite incorrect parameters

I have a strange problem using .htaccess RewriteEngine.

I have this simple rule:

RewriteEngine On
RewriteRule ^(.\*)/(.\*).php$ pages/test.php?lang1=$1&page1=$2 [L]

In pages/test.php I put this php code:

echo('lang: '.$_GET['lang1'].'< br />');    
echo('page: '.$_GET['page1'].'< br />');    
echo('querystring: '.$_SERVER['QUERY_STRING']);

So, when calling http://test.local/en-US/something.php, I would expect something like:

lang: en-US    
page: something    
querystring: lang1=en-US&page1=something

Instead, this is the weird output I get from the page:

lang: pages    
page: test    
querystring: lang1=pages&page1=test

Can someone help me out?

like image 587
Cuttlefish Avatar asked May 28 '26 13:05

Cuttlefish


2 Answers

You get that result because you created a loop, try using these lines :

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/pages/test\.php$
RewriteRule ^(.*)/(.*)\.php$ /pages/test.php?lang1=$1&page1=$2 [L]
like image 87
Oussama Jilal Avatar answered May 31 '26 05:05

Oussama Jilal


You can also use the QSA flag:

RewriteRule ^(.*)/(.*).php$ pages/test.php?lang1=$1&page1=$2 [QSA,L]

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

http://httpd.apache.org/docs/current/rewrite/flags.html

like image 38
Maurice Avatar answered May 31 '26 04:05

Maurice