Forum Moderators: phranque
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]
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]
http://www.example.com/bob/bobs-house-of-fish/bait.html
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]
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} ^([^,]+)
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]
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!
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]
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.