mod_rewrite - Redirection problem: how do I map Ids to countries?

Hi,
I’m new with mod_rewrite and I’m having a problem.
I need to redirect
mysite/en/guides?country=275
to
mysite/en/guides/Italy

The question is: how could I map 275 to Italy ?

If you have access to the server config httpd.conf you can use RewriteMap.
If you don’t you’d have to write everything out manually – write an Alias for each and every country. Bit tedious, but of course it’s a one time only thing :slight_smile:

Could you write down an example please ( for example for 2 countries )?
Thanks in advance


RewriteEngine On
RewriteCond %{QUERY_STRING} country=275
RewriteRule ^en/guides http://mysite/en/guides/Italy? [L,R=301]

RewriteCond %{QUERY_STRING} country=211
RewriteRule ^en/guides http://mysite/en/guides/USA? [L,R=301]

I have no idea what id’s a and countries you have in your site so the second block for the USA is just an example.

Can I do something like (regex not perfect):


RewriteCond %{QUERY_STRING} country=([275 | 211) #any number
RewriteRule ^en/guides http://mysite/en/guides/[ Italy | Usa]   ? [L,R=301]

… and say 275 belongs to Italy, 211 belongs to Usa and so on ?

sadly, no

I’m trying to use rewritemap to solve this.
I have apache conf access.
I have defined a map.txt like this:


275 italy
211 usa

I have defined:


RewriteMap country txt:/var/map.txt

…restart apache and there is no errors.

In my htaccess:


RewriteCond %{QUERY_STRING} country=([0-9]+)
RewriteRule ^en/guides http://mysite/en/guides/${country:$1|1}? [L,R=301]

So, what I want is when someone sets:

http://mysite/en/guides?country=211

She/He will be redirected to:

http://mysite/en/guides/usa

Currently I’m getting this from my previous rule:

http://mysite/en/guides/1?country=211

… and of course this is not what I want.

What I’m missing here ?

You’re using $1 in the RewriteRule, but there is no $1.
A $1 could be made by using parenthesis in the left hand side of the RewriteRule. See any introduction on mod_rewrite for more info on this :slight_smile:

What you need to do is give it %1 , the country that was matched in the RewriteCond

So, instead of


RewriteCond %{QUERY_STRING} country=([0-9]+)
RewriteRule ^en/guides http://mysite/en/guides/${country:[COLOR="Red"]$1[/COLOR]|1}? [L,R=301]

use


RewriteCond %{QUERY_STRING} country=([0-9]+)
RewriteRule ^en/guides http://mysite/en/guides/${country:[COLOR="Red"]%1[/COLOR]|1}? [L,R=301]

Oh, thanks, it’s getting closer :slight_smile:
Now its working but it’s concatenating the QUERY_STRING at the end:

en/guides/netherlands?country=275

… but it should be just:

en/guides/netherlands

Thanks for all your support :slight_smile:

Are you sure you have the ? at the end of the rule ? That should prevent Apache from copying the query string to the new location.


RewriteCond %{QUERY_STRING} country=([0-9]+)
RewriteRule ^en/guides http://mysite/en/guides/${country:%1|1}[COLOR="Red"]?[/COLOR] [L,R=301]

If you do, there’s probably something else going on. Could you please post the complete .htacces in that case?

you were right, ? left.
Thanks !!!