I'm redesigning the URL of a PHP multilingual site (en|es|de|fr|ru). The URL's of the site are this way:
www.mysite.com/page
www.mysite.com/page/subpage1
www.mysite.com/page/subpage1/subpage2
www.mysite.com/page/subpage1/subpage2/subpage3
Up to four levels of subdirectories (products, subproducts, etc.). The language is passed as a GET parameter:
www.mysite.com/page?lang=es
or
www.mysite.com/page/subpage1/subpage2?lang=de
The current .htaccess is something like this:
Options +FollowSymlinks +MultiViews -Indexes
RewriteEngine on
RewriteBase /
# Redirect all versions of homepage to www.mysite.com
RewriteCond %{REQUEST_URI} ^/index\.html$
RewriteRule ^index.html$ http://www.mysite.com/ [R=301,L]
# Redirect non-www to www traffic
RewriteCond %{HTTP_HOST} !^(www\.mysite\.com)?$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#Remove php extension
RewriteCond %{REQUEST_URI} ^(.*).php$
RewriteRule ^(.*)$ %1 [R=301,QSA]
#Here I have a lot of 301 redirections, I ommit them for this example
# First level redirections
RewriteRule ^([0-9a-zA-Z\-\_]+)$ view?page=$1 [L,QSA]
# Second level redirections
#RewriteRule ^(page1|page2|page3)\/([0-9a-zA-Z\-\+\_\,\.\(\)]+)$ view?page=$1&subpage1=$2 [L,QSA]
# Third level redirections
RewriteRule ^([0-9a-zA-Z\-\_]+)\/([0-9a-zA-Z\-\+\_\,\.\(\)]+)\/([0-9a-zA-Z\-\_]+)$ view?page=$1&subpage1=$2&subpage2=$3 [L,QSA]
# Fourth level redirections
RewriteRule ^([0-9a-zA-Z\-\_]+)\/([0-9a-zA-Z\-]+)\/([0-9a-zA-Z\-]+)\/([0-9a-zA-Z\-]+)$ view?page=$1&subpage1=$2&subpage2=$3&subpage3=$4 [L,QSA]
Now I want to redirect the whole site to a URL with a prepended language subdirectory if there is no language parameter in the URL (GET), like this (default english language):
www.mysite.com/page -> www.mysite.com/en/page
www.mysite.com/page/subpage1 -> www.mysite.com/en/page/subpage1
and if the GET parameter is passed in the URL, I want to redirect to the correct URL:
www.mysite.com/page?lang=es -> www.mysite.com/es/page
www.mysite.com/page/subpage1?lang=es -> www.mysite.com/es/page/subpage1
I've reviewed other similar questions but all of them are based in a 301 redirect with last parameter ([R=301,L]), and I'm afraid that the Last is not valid for me.
I have a few questions:
Thanks in advance
You can redirect your whole site to English as your default or to the correct localized version of your website, with doing something like this:
RewriteRule ^(en|es|de)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
You need these additional rules:
# lang supplied in query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})(?:&|$) [NC]
RewriteRule !^[a-z]{2}/ /%1/%{REQUEST_URI}? [L,NC,R=301]
# lang not supplied in query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^lang=.+(&|$) [NC]
RewriteRule !^en/ /en/%{REQUEST_URI} [L,NC,R=301]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With