Solved

htaccess RewriteRule using domain name for language as a parameter.

Posted on 2010-08-16
3
587 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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é.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 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

12 Experts available now in Live!

Get 1:1 Help Now