Friendly URLs and Default Languages

New to mod_rewrite? This is a good place to start.

Friendly URLs and Default Languages

Postby napper » Sun Jun 14, 2009 12:28 am

Hi everybody,

I am going crazy with trying something that is probably really simple I just can't figure out where my syntax is wrong. Sitation:

I want urls to look like this:

www . mypage . com/en/topic1/subtopic10.html

which is supposed to translate into

/index.php?path=$1&lang=1

This is working fine. I have set this up for three languages: en, fr, and de using rules like this:

RewriteRule ^en/(.*).html /index.php?path=$1&lang=1

However, since we are just implementing multi-language options on the site our old links looked like this:

www . mypage . com/topic1/subtopic10.html -> without the language flag. I want the old links to translate automatically into the default language, meaning if someone enters

www . mypage . com/topic1/subtopic10.html it automatically assumes: /index.php?path=$1&lang=1

So the .htaccess has to check if a language flag exists in the url before executing a Rewrite Rule, right? I thought it would look something like this:

RewriteCond %{REQUEST_URI} ^en/.*\.html [NC]
RewriteRule ^en/(.*).html /index.php?path=$1&lang=1 [L]

Basically what I want is this:

IF URL contains (right at the beginning) /en/ -> then RewriteRule ^en/(.*).html /index.php?path=$1&lang=1

IF URL contains (right at the beginning) /de/ -> then RewriteRule ^de/(.*).html /index.php?path=$1&lang=2

IF URL contains (right at the beginning) /fr/ ->RewriteRule ^fr/(.*).html /index.php?path=$1&lang=3

ELSE RewriteRule RewriteRule ^(.*).html /index.php?path=$1&lang=1

How far away from the solution am I? Can someone help?

Kind regards,

Napper
napper
 
Posts: 3
Joined: Sun Jun 14, 2009 12:13 am

Postby richardk » Sun Jun 14, 2009 10:56 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^en/(.+)\.html$ /index.php?path=$1&lang=1 [QSA,L]
RewriteRule ^de/(.+)\.html$ /index.php?path=$1&lang=2 [QSA,L]
RewriteRule ^fr/(.+)\.html$ /index.php?path=$1&lang=3 [QSA,L]

RewriteRule ^(.+)\.html$ /index.php?path=$1&lang=1 [QSA,L]


Or you could redirect to the new URL (/topic1/subtopic10.html to /en/topic1/subtopic10.html)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond $1 !^(en|de|fr)$
RewriteRule ^([^/]+)(/.+)?\.html$ /en/$0 [R=301,L]

RewriteRule ^en/(.+)\.html$ /index.php?path=$1&lang=1 [QSA,L]
RewriteRule ^de/(.+)\.html$ /index.php?path=$1&lang=2 [QSA,L]
RewriteRule ^fr/(.+)\.html$ /index.php?path=$1&lang=3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby napper » Sun Jun 14, 2009 7:46 pm

Hi richardk,

this works like a charm. Both solutions are cool, I am thinking of taking the upper one, at least in the beginning, as it still displays the old strukture the old way, while still allowing for a multilingual site. I think I understand the context to, though I don't know why you use a (.+) instead of a (.*)

Thank you very much for your help!!!
napper
 
Posts: 3
Joined: Sun Jun 14, 2009 12:13 am

Postby richardk » Mon Jun 15, 2009 11:37 am

I don't know why you use a (.+) instead of a (.*)

* means "0 or more" and + means "1 or more" therefore .* will match a request to /en/.html, but .+ will not. Depending on what characters your path variable is allowed to contain, your could use a limited character class instead to match less invalid URLs.

http://www.regular-expressions.info/repeat.html
http://www.regular-expressions.info/charclass.html
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby napper » Mon Jun 15, 2009 7:42 pm

Ah - I'm beginning to understand. The .htaccess file is one of those things I touch maybe once a year, when new code makes it necessary. :) This was the first time in 4 years that I could not figure it out myself. Maybe I am getting old... :)

Anyway, again - thanks a bunch! Of to PHP again!
napper
 
Posts: 3
Joined: Sun Jun 14, 2009 12:13 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 96 guests

cron