user7424312 user7424312 - 3 years ago 49
Apache Configuration Question

Rewrite Rule not working back reference

I have this example :

RewriteCond %{QUERY_STRING} game=MyGame
RewriteRule ^games/freeGames?$ /flash/games/%1? [L,R=301]


I'm redirected to the homepage. But if I use :

RewriteCond %{QUERY_STRING} game=MyGame
RewriteRule ^games/freeGames?$ /flash/games/MyGame? [L,R=301]


Redirect works fine. Can you help me please ?

Another question : is possible to have in
%{QUERY_STRING}
and array ? For example MyGame, OtherGame, Biliard, etc ?

Thx in advance and sorry for my english

Answer Source
RewriteCond %{QUERY_STRING} game=MyGame
RewriteRule ^games/freeGames?$ /flash/games/%1? [L,R=301]

In this code, the %1 backreference is always empty, since you have not defined a captured group (ie. parenthesised subpattern) in the preceding RewriteCond directive. So, the resulting URL is always /flash/games/ (your "homepage" I presume).

You need to define the captured group by surrounding the string/regex in parenthesis), for example:

RewriteCond %{QUERY_STRING} game=(MyGame)

Another question : is possible to have in %{QUERY_STRING} and array ? For example MyGame, OtherGame, Biliard, etc ?

If you need to match multiple items then you can use alternation in the captured group. For example:

RewriteCond %{QUERY_STRING} game=(MyGame|OtherGame|Biliard|etc.)

Now it will match "MyGame" or "OtherGame" or "Biliard" etc, and whatever matches is saved in the %1 backreference.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download