SEO-friendly URLs for multilingual site

Discuss practical ways rearrange URLs using mod_rewrite.

SEO-friendly URLs for multilingual site

Postby Rackne » Mon Oct 06, 2008 10:39 am

Hi,
I am working on a multilingual site with the links like:
www.example.com/en/index.html
www.example.com/en/sitemap.html
www.example.com/de/index.html
etc.
The php-files shall obviously be named en.index.php, de.sitemap.php etc.
Could you help me sort out the modrewrite rule for it?
Rackne
 
Posts: 7
Joined: Mon Oct 06, 2008 10:32 am

Postby richardk » Mon Oct 06, 2008 11:32 am

Why is that obvious? If you're going to use seperate files why not use .php and put the files in /en and /de sub directories?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(en|de)/([^/]+)\.html$ /$1.$2.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Rackne » Mon Oct 06, 2008 12:04 pm

Worked fine, thanks a million!
I forgot to say that I only have like 8-10 pages for each language, so it's easier to keep track of the files when they are in the same folder. But for different folders it has to be something like:
Code: Select all
RewriteRule ^(en|de)/([^/]+)\.html$ /$1/$2.php [QSA,L]

?
One more thing: how do I get language specific 404 error page?
Rackne
 
Posts: 7
Joined: Mon Oct 06, 2008 10:32 am

Postby richardk » Mon Oct 06, 2008 1:39 pm

But for different folders it has to be something like:
Code: Select all
RewriteRule ^(en|de)/([^/]+)\.html$ /$1/$2.php [QSA,L]

?

Yes. Then you just need to change to .php and the mod_rewrite is no longer required. My point was that it doesn't really seem worth it if you're going to have well named files (it's already sitemap.php).

One more thing: how do I get language specific 404 error page?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$1.$2.php !-f
RewriteRule ^(en|de)/([^/]+)\.html$ /$1.404.php [QSA,L]

RewriteRule ^(en|de)/([^/]+)\.html$ /$1.$2.php [QSA,L]

Make sure you send a 404 header in your PHP
Code: Select all
header('Content-Type: text/html', true, 404);
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Rackne » Tue Oct 07, 2008 3:36 am

Works fine, but with 2 more questions:
1. It does not account for bad links like www.example.com/en/
Would it be correct to
Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$1.$2.php !-f
RewriteCond %{DOCUMENT_ROOT}/$1/ !-d
RewriteRule ^(en|de)/([^/]+)\.html$ /$1.404.php [QSA,L]
RewriteRule ^(en|de)/$ /$1.404.php [QSA,L]
RewriteRule ^(en|de)/([^/]+)\.html$ /$1.$2.php [QSA,L]
?
2. It keeps wrong URL in the address bar. Is that correct or would it be better to
Code: Select all
RewriteCond %{DOCUMENT_ROOT}/$1.$2.php !-f
RewriteCond %{DOCUMENT_ROOT}/$1/ !-d
RewriteRule ^(en|de)/([^/]+)\.html$ /$1/404.html [R,L]
RewriteRule ^(en|de)/$ /$1/404.html [R,L]
RewriteRule ^(en|de)/([^/]+)\.html$ /$1.$2.php [R,L]
?
Thanks in advance!
Rackne
 
Posts: 7
Joined: Mon Oct 06, 2008 10:32 am

Postby richardk » Tue Oct 07, 2008 11:26 am

1. It does not account for bad links like www.example.com/en/

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(en|de)/[^/]+\.html$ [NC]
RewriteRule ^(en|de)(/.*)?$ /$1.404.php [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/$1.$2.php !-f
RewriteRule ^(en|de)/([^/]+)\.html$ /$1.404.php [QSA,L]

RewriteRule ^(en|de)/([^/]+)\.html$ /$1.$2.php [QSA,L]


2. It keeps wrong URL in the address bar. Is that correct or would it be better to

You shouldn't do that. It won't sent a 404 status, it will send a redirect status.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Rackne » Wed Oct 08, 2008 8:33 am

Thanks!
Rackne
 
Posts: 7
Joined: Mon Oct 06, 2008 10:32 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 8 guests

cron