Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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]
share|improve this answer
 
It works like a charm, thank you! I don't really understand why it went through a loop, though... –  Alessandro Aug 27 '12 at 15:27
 
you redirect ^(.*)/(.*)\.php$ , pages/test.php matches the pattern too. anyway, glad it is now working :) –  Oussama Aug 27 '12 at 15:33
add comment

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

share|improve this answer
 
Thank you, I tried the QSA flag but that was the result: lang1=pages&page1=test&lang1=en-US&page1=dfsdfasdf. I guess it's because of the loop problem above... –  Alessandro Aug 27 '12 at 15:34
 
I reproduced your problem here, and it gave the right result. The L flag normally ends any further processing. If the other answer worked out, it's fine too :) –  Maurice Aug 27 '12 at 15:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.