Solved

htaccess RewriteRule using domain name for language as a parameter.

Posted on 2010-08-16
3
592 Views
Last Modified: 2012-05-10
hi,
   Im managing a bilingual website.

Two languages can be used with URL like this.

www.website.com/product/computer.html?lang=en
www.website.com/product/computer.html?lang=jp
Also
www.website.com/product/apple/laptop.html?lang=en
www.website.com/product/apple/laptop.html?lang=jp


No other pages/url_patterns in the website needs Language settings.
I need a redirect rule to use with this 2 patterns with language in domain part of URL.

en.website.com/product/computer.html
should be processed internally as
www.website.com/product/computer.html?lang=en

and

jp.website.com/product/apple/laptop.html
should be processed as
www.website.com/product/apple/laptop.html?lang=jp

As you can see
domain part en/jp should become ?lang=en/jp
and should be valid with this URL patterns
xxxx/product/xxxxxx
AND
xxxx/product/apple/xxxxx

RewriteRule can be in httpd.conf OR htaccess.

Since Im not good at this RewriteRule I request you to have this as simple as possible.

Thank you for all in advance.

0
Comment
Question by:BToTheAToTheBABA
  • 2
3 Comments
 
LVL 26

Assisted Solution

by:arober11
arober11 earned 500 total points
ID: 33447580
Try:

RewriteEngine On
RewriteCond %{QUERY_STRING}  !lang=[ej][np]  [NC]
RewriteCond %{REQUEST_URI}  ^/product/         [NC]
RewriteCond %{HTTP_HOST}      ^(en|jp)\.         [NC]
RewriteRule  .*   %{REQUEST_URI}?lang=%1      [L,QSA]
0
 

Author Comment

by:BToTheAToTheBABA
ID: 33519466
hi, This is working fine.

Can you explain these lines ?

RewriteCond %{QUERY_STRING}  !lang=[ej][np]  [NC]
RewriteCond %{REQUEST_URI}  ^/product/         [NC]
RewriteCond %{HTTP_HOST}      ^(en|jp)\.         [NC]
RewriteRule  .*   %{REQUEST_URI}?lang=%1      [L,QSA]
0
 
LVL 26

Accepted Solution

by:
arober11 earned 500 total points
ID: 33525207
Sure:

# Turn on the Apache Rewrite Engine
RewriteEngine On    

# If the URL DOSE NOT contain a CGI-Parameter of "lang=XX", where XX is either {en, ep, jn, jp},  ignoring case (NC = NOT CASE sensitive)
RewriteCond %{QUERY_STRING}  !lang=[ej][np]  [NC]

# AND If the URL starts with "/Product/", in any case
RewriteCond %{REQUEST_URI}  ^/product/         [NC]

# AND If the URL starts with http://en.   or   http://jp.
RewriteCond %{HTTP_HOST}      ^(en|jp)\.         [NC]

# THEN Add (QSA = Query String Append) a "lang=XX" to the URL, and stop manipulating the URL (L = LAST rule)
RewriteRule  .*   %{REQUEST_URI}?lang=%1      [L,QSA]


See: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
This article discusses four methods for overlaying images in a container on a web page
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now