Subdirectories as languages and forcing default language

Discuss practical ways rearrange URLs using mod_rewrite.

Subdirectories as languages and forcing default language

Postby ralex » Tue Aug 11, 2009 2:56 am

i have multi-language site where languages are actually subdirectories and i want to force default language.

currently, in web root i have this in .htaccess:
Code: Select all
RewriteEngine On
RewriteBase /path/to/site/

RewriteCond %{REQUEST_URI} !^(sr|en)(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ sr/$1 [L,QSA,R=301]


and i have sr and en subfolders (languages) in both those directories i have following .htaccess (with appropriate RewriteBase):
Code: Select all
RewriteEngine On
RewriteBase /path/to/site/sr/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]


end when i go to:
http://example.com/path/to/site/
it will redirect me to:
http://example.com/path/to/site/sr/
(which is what i want)

but, when i go to:
http://example.com/path/to/site
(without slash at end) it redirects me to:
http://example.com/path/to/site/sr/absolute/file/system/path/to/web/root


thanks in advance!
ralex
 
Posts: 3
Joined: Tue Aug 11, 2009 2:47 am
Location: Serbia

Postby richardk » Wed Aug 12, 2009 11:04 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteBase /path/to/site/

RewriteCond $1 !^(sr|en)(/.*)?$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ sr/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby ralex » Wed Aug 12, 2009 11:29 pm

Thanks, I've tried that but with no luck...

now when I go to:
http://example.com/path/to/site
(without slash at end) it redirects me to:
http://example.com/path/to/site/sr//absolute/file/system/path/to/web/root
ralex
 
Posts: 3
Joined: Tue Aug 11, 2009 2:47 am
Location: Serbia

Postby richardk » Fri Aug 14, 2009 1:02 pm

OK, try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/path/to/site/(sr|en)(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} ^/path/to/site(/(.*))?$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /path/to/site/sr/%2 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby ralex » Sat Aug 15, 2009 5:45 am

thank you very much, it worked now.

it just didn't work when i try to access:
http://example.com/path/to/site/somepage

but i've tweaked it a bit and it worked like a charm. this is what works for me:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/path/to/site/(sr|en)(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} ^/path/to/site/(.*)?$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /path/to/site/sr/%1 [R=301,L]


thank you once again!
ralex
 
Posts: 3
Joined: Tue Aug 11, 2009 2:47 am
Location: Serbia


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 22 guests

cron