Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite problem for a multilingual site

         

tomasv

2:51 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



Hi,
I have a problem that I think can be solved with mod_rewrite but I havent found a good solution yet so now I turn to you :)

I run a multilingual site. The language system is totaly transparent at the moment so the URL's are exactly the same for all languages. The language used is controlled by PHP sessions. The problem with this design is that search enginges only spider the main language and im trying to solve that. I don't want to use country specific top domains for each language or subdomains since its a site where people from different countries interact with eachother.

So what I want is to use mod_rewrite in some way so the url looks like I have a directory for each language. If you for example are using german language the url will be www.sitename.com/DE/index.php and so on.
One idea I have is to translate the url to "index.php?lang=DE" but the site got hundreds of pages and more gets added often so having one rewrite rule for every page is not really doable and I would like to avoid having to always send a GET-value (lang) to all pages if possible. So I guess I have three questions:

- Would it be possible to somehow set a apache environmental variable from PHP that mod_rewrite could use to make the url look like it uses a subdirectory for each language?

- Is it possible to write a rule to always translate a url like www.sitename.com/DE/index.php to index.php?lang=DE for all the pages and that works even if there are other GET values sent to the page (ie. index.php?f=2&d=1&lang=DE.

- Am I totaly out in the blue and there are other really smart solutions to my problem? :)

Thanks alot in advance!

kindest regards
Tomas

jdMorgan

5:33 am on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



tomasv,

Welcome to WebmasterWorld!

> Is it possible to write a rule to always translate a url like www.sitename.com/DE/index.php to index.php?lang=DE for all the pages and that works even if there are other GET values sent to the page (ie. index.php?f=2&d=1&lang=DE.


RewriteCond %{QUERY_STRING} !lang=[A-Z]{2}
RewriteRule ^([A-Z]{2}/index\.php$ /index.php?lang=$1 [QSA]

You'll have to test that or read the documentation to see what's going on with it, but that is one way to convert yourdomain.com/ZG/index.php?grazzi=danke to a script call of /index.php?lang=ZG&grazzi=danke

The code will not rewrite if the lang= parameter is already present, so this prevents a possible infinite-loop problem.

Jim

tomasv

9:03 am on Feb 10, 2005 (gmt 0)

10+ Year Member



Hi and thanks for the reply!
The problem with that rule is that it will only work for index.php right? What I would prefer is one statement that will work for all files, if possible. Since the site got alot of files and more get added all the time.

If its not possible to have one rule for all files is it how much of a cpu hog is mapping a textfile with all the filenames to mod_rewrite?

Thanks in advance!

Caterham

12:57 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



yes, you can make it some more flexible:

RewriteCond %{QUERY_STRING}!lang=[A-Z]{2}
RewriteRule ^([A-Z]{2})/(.+)\.php$ /$2.php?lang=$1 [QSA,L]

jdMorgan

6:01 pm on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The problem with that rule is that it will only work for index.php right?

What problem?... That is what you asked for, isn't it? :)

Jim