SeinopSys
SeinopSys

Reputation: 8937

.htacces redirection with query strings

I created a .htacces file which does the following:

Then

But..

I want to preserve the query string at the end, which would mean visiting /.[p]/[l]?a=b results in a request to /index.php?page=[p]&lang=[l]&a=b

I cannot seem to get the regular expressions working for this. Here's the whole .htaccess file:

RewriteEngine On
RewriteBase /Main/djdavid98/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(hu|en)?(\/?(\?(([\S]+\=[\S]+)*))?)?$ ./.index/$1$3 [R=301,L]
RewriteRule ^\.([\w\d]+)(\/(hu|en))?\/?(\?(([\S]*=[\S]*)+))?$ index.php?page=$1&lang=$3&$5

Upvotes: 1

Views: 44

Answers (1)

Jon Lin
Jon Lin

Reputation: 143866

Use the QSA flag for RewriteRule which will append any query string that's already there to the end of the one you've constructed in your rule's target:

RewriteRule ^\.([\w\d]+)(\/(hu|en))?\/?(\?(([\S]*=[\S]*)+))?$ index.php?page=$1&lang=$3&$5 [L,QSA]

Note that query strings get appended to the end automatically, except when you've constructed your own in your rule's target. That's why the first rule will preserve the query string, but the second rule won't.

Upvotes: 1

Related Questions