Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite for localization site

         

mgorlero

1:55 am on Apr 21, 2005 (gmt 0)

10+ Year Member



Hi,

Im having a big problem to make things done with the mod_rewrite.
I have a website that is multilingual and i want that "http://www.example.com/spa/argentina/" go to "http://www.example.com/home/index.php"

The same for all the coutries i have
"http://www.example.com/spa/uruguay/" -> "http://www.example.com/home/index.php"
"http://www.example.com/eng/jamaica/" -> "http://www.example.com/home/index.php"
...

But the other problem is how to keep this format in the url, when the user goes for example to "http://www.example.com/products/index.php?id=123"
show them "http://www.example.com/spa/uruguay/products/index.php?id=123"

is it possible to do this?

Thanks!

[edited by: jdMorgan at 4:07 am (utc) on April 21, 2005]
[edit reason] Examplified URLs. [/edit]

sitz

2:15 am on Apr 21, 2005 (gmt 0)

10+ Year Member



The same for all the coutries i have
"http://www.example.com/spa/uruguay/" -> "http://www.example.com/home/index.php"
"http://www.example.com/eng/jamaica/" -> "http://www.example.com/home/index.php"

This part is fairly straightforward. Well, kind of. Getting it working is easy enough, getting it working so that it *only* affects what you need affected is a bit trickier. One can start with:


RewriteEngine on
RewriteRule ^/[^/]{3}/[^/]+/ /home/index.php [L]

Of course, this assumes that all your language codes are three letters (you may want to consider using two-letter codes, or the 'xx-xx' format, since there are standards for those). The downside to the above is that it would also catch requests like:

http://www.example.com/bob/bobs-house-of-fish/bait.html

You could make it more specific...

RewriteEngine on
#
# match on a language as the first part of the path,
# followed by a '/', followed by any number of
# non-slash characters, followed by a final '/'
RewriteRule ^/(eng¦spa¦ger¦rus)/[^/]+/ /home/index.php [L]

...but this means that you'd need to update your RewriteRule whenever you added a new language. Finally, both of the above methods will leave the URL the user requested in the Location bar; this may or may not be what you want. If it's not, you'll need to do something like this:

RewriteEngine on
RewriteRule ^/(eng¦spa¦ger¦rus)/[^/]+/ http://www.example.com/home/index.php [L,R]

As for the second part, that's a bit tricker (to say the least); you say you want to map "http://www.example.com/products/index.php?id=123" to "http://www.example.com/spa/uruguay/products/index.php?id=123"; how do you know, from the request you received, which country's page to show them? You could *try* doing this in mod_rewrite, using the Accept-Language header (note: this code will NOT WORK as there is NO RewriteRule in it):


RewriteEngine on
#
# verify that the accept language header exists and is
# non-empty
RewriteCond %{HTTP_ACCEPT_LANGUAGE} .
#
# capture the first element in the Accept-Language
# header in the '%1' backreference
RewriteCond %{HTTP_ACCEPT_LANGUAGE} ^([^,]+)

But that only gives you the international two- or four-letter code for the language the browser is willing to accept, and may NOT be what you want. You could ask the user to choose a country/language combo and then set a cookie with those preferences; that's certainly the easiest of your available options. You could also look into Apache's language negotiation [httpd.apache.org], which might be of help.

Does this help at all, or only add to the confusion? =)

[edited by: jdMorgan at 4:11 am (utc) on April 21, 2005]
[edit reason] Examplified quoted text. [/edit]

mgorlero

10:26 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



Thank you so much Sitz.
ive used:
RewriteRule ^/(eng¦spa¦ger¦rus)/[^/]+/ /home/index.php [L]
and it is working great.

Last question, how it affects mod_rewrite a web site stats service, i mean, the visits report it will show me the real url or the [example.com...]

And for the images paths, i need to make them all absolute there is anyway to keep them relative to show them?

Thank you again!

sitz

12:47 am on Apr 22, 2005 (gmt 0)

10+ Year Member



RewriteRule ^/(eng¦spa¦ger¦rus)/[^/]+/ /home/index.php [L]

Three things to note about this:

1) the '¦' bar needs to be replaced by the solid bar (if you've not already done so; I assume you have, since you say it's working, but I wanted to mention it);

2) you don't need '¦ger¦rus' if you don't use those languages. Likewise, if you have other languages you need to add there, you can do so.

3) Thinking on it, you should probably tweak that Rule so that it looks something like this:


RewriteRule ^/(eng¦spa¦ger¦rus)/[^/]+/? /home/index.php [L]

(the question mark makes that last '/' optional)

With regards to stats, the entry in the access log will the request as it was made originally, NOT what it looks like after Rewrite'ing.

As far as images go, I have no way of knowing what will and will not work without examples of where the images are referenced on the filesystem, and how they are called in the HTML.

If you haven't already, however, I'd read the mod_rewrite documentation several times, and read up on regular expressions; Linux has the regex(7) manpage, and the 'Mastering Regular Expressions' is very good (and now in it's second edition). And experiment! That's really the best way to learn. The following directives are quite helpful during the learning process (and during debugging):


RewriteLog /path/to/rewrite.log
RewritelogLevel 9

Note that you should NOT use these on a production host; mod_rewrite will log copious amounts of data, slowing down Apache and filling your disk.