Forum Moderators: phranque

Message Too Old, No Replies

URL redirection/modification based on domain or TLD

.htaccess redirection based on domain / language selection by domain tld

         

Yatko

10:40 pm on May 12, 2009 (gmt 0)

10+ Year Member



Hi,

I have a site, and two domains pointing to it - one parked on top of main domain - (both domains pointing to /home/public_html/example.com folder). What I am trying to achieve is language selection using .htaccess

The language selection works like this:
if the URL ends with /index.php?lang=ro or &lang=ro -the site will use RO translation
if the URL ends with /index.php?lang=en or &lang=en -the site will use EN(defa) translation

I am trying to redirect all requests that come both from example.ro and www.example.ro to www.example.ro/index.php?lang=ro

I managed to redirect www.example.ro to example.ro/index.php?lang=ro (without www), but I am stuck here. I am unable to redirect www.example.ro to www.example.ro/index.php?lang=ro and cannot add additional rule in case of a visit without www (example.ro). I am sure, my rule is OK, just not the right rule for this case :( I thing my approach could be wrong.

RewriteCond %{HTTP_HOST} ^www\.example\.ro$ [NC]
RewriteRule ^(.*)$ http://example.ro/index.php?lang=ro$1 [R=301,L]

I started to experiment with the rule below (made for directories), but could not manage to change it to work with URLs.

#two domains served from one root..
RewriteCond %{HTTP_HOST} example.com
RewriteCond %{REQUEST_URI} !^/com
RewriteRule ^(.*)$ com/$1 [L]

RewriteCond %{HTTP_HOST} example.ro
RewriteCond %{REQUEST_URI} !^ro
RewriteRule ^(.*)$ ro/$1 [L]

Any help wold be appreciated! Thanks!

[edited by: coopster at 11:22 pm (utc) on May 12, 2009]
[edit reason] please use example.com, thanks! [/edit]

g1smd

11:54 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What will be served to a visitor requesting either
www.example.ro/index.php
or
www.example.ro/
exactly like that?

I suspect you haven't fully thought out the requirements here, and have moved on to the coding phase before knowing exactly what you want the code to do for all possible request formats that could be thrown at it.

I am especially confused that some of the code says .ro and some says .com too.

Additionally, you have some steps missing, as you'll see when you realise that there are some URL formats that none of your rules cater for.

I would say that you URL formats are "too complicated" and that in some cases what you call a redirect should in fact be a rewrite.

You do not need the string "index.php" to appear in any of your URLs.

You do need to consider what you want to serve when the language is not specified in the URL request hitting the server.

You do need to look at the Duplicate Content impact that will have on your site.

Yatko

10:03 am on May 13, 2009 (gmt 0)

10+ Year Member



Hi g1smd,

Yes, I know this code is not the right one and yes, a rewrite is needed instead of redirect, "index.php" isn't a must either.

I have a website up and running with the option for two languages. The language can be selected by adding &lang=XX to the url or simply adding ?lang=XX after the domain name (yatko.com/?lang=XX).

Now I have the second domain (yatko.ro) parked on top of yatko.com (not working live now). No matter that you access yatko.com or yatko.ro the whole thing and url structure is the same (yes it is duplicate content).

My goal is, when somebody is accessing yatko.com/anyURL or www.yatko.com/anyURL, the URL is rewritten to the default language: www.yatko.com/anyURL&lang=en or if (www.)yatko.com is accessed, the rewrite is www.yatko.com/index.php?lang=en (it could be www.yatko.com/?lang=en -without index.php too).

In case, that somebody accesses any URL using the second domain ex. yatko.ro/anyURL or www.yatko.ro/anyURL the URL is rewritten to www.yatko.ro/anyURL?lang=ro respective www.yatko.com/index.php?lang=ro

Simplified: when the tld is .ro rewrite the url so it ensd with "&lang=ro", when the tld is .com rewrite the url to end with "&lang=en".

In this case the url will determine the language used and since the whole content is translated, I won't have duplicate content any more. English content on www.yatko.com/..... Romanian content on www.yatko.ro/......

Is it at lest possible?

Thank you!

jdMorgan

8:00 pm on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're probably gonna love figuring this out...
(and please don't try to use it until you understand it -- all of it.)

This is what it takes to do what you seem to want to do, and to clean up the existing search results as well. Don't try to use this except for testing until you have changed all links on your site to point to "/" instead of "/index.php".

This is actually only a partial solution, as certain combinations of problems in requested URLs can lead to multiple (chained) redirects. But a full solution is twice as complicated...


DirectoryIndex index.php
#
Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# Externally redirect to remove the language-selector query parameters from URLs on the Web
#
# for .ro
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?]*\?(([^&]*)&)*lang=ro(&[\ ]*)?\ HTTP/
# accept optional parm(s) before lang name/value, but none after
RewriteCond %{QUERY_STRING} ^(([^&]*(&[^&]*)*)&)?lang=ro$ [OR]
# require parm(s) before and accepts optional parm(s) after lang name/value
RewriteCond %{QUERY_STRING} ^(([^&]*(&[^&]*)*)&)lang=ro((&[^&]*)*)$
RewriteRule ^(.*)$ http://www.example.ro/$1?%2%4 [R=301,L]
#
# for .com
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?]*\?(([^&]*)&)*lang=en(&[\ ]*)?\ HTTP/
# accept optional parm(s) before lang name/value, but none after
RewriteCond %{QUERY_STRING} ^(([^&]*(&[^&]*)*)&)?lang=en$ [OR]
# require parm(s) before and accepts optional parm(s) after lang name/value
RewriteCond %{QUERY_STRING} ^(([^&]*(&[^&]*)*)&)lang=en((&[^&]*)*)$
RewriteRule ^(.*)$ http://www.example.com/$1?%2%4 [R=301,L]
#
# Externally redirect (only) direct client requests for /index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(\?[^\ ]*)?\ HTTP/
RewriteCond %{HTTP_HOST} example\.([b]ro¦com[/b])
RewriteRule ^index\.php$ http://www.example.%1/ [R=301,L]
#
# Externally redirect requests for non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} example\.ro [NC]
RewriteCond %{HTTP_HOST} !=www.example.ro
RewriteRule ^(.*)$ www.example.ro/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ www.example.com/$1 [R=301,L]
#
# Internally rewrite to use TLD as language selector in script query string
RewriteCond %{HTTP_HOST} =www.example.com
RewriteCond %{QUERY_STRING} !&?lang=
RewriteRule ^$ /index.php?lang=en [QSA,L]
#
RewriteCond %{HTTP_HOST} =www.example.ro
RewriteCond %{QUERY_STRING} !&?lang=
RewriteRule ^$ /index.php$1?lang=ro [QSA,L]

Replace the broken pipe "¦" character (see bold font) in the code above with a solid pipe character before use; Posting on this forum modifies the pipe characters.

I should also point out that this code isn't tested, and I just typed it in. There may indeed be typos or errors in it...

Have fun!
Jim

Yatko

4:44 pm on May 14, 2009 (gmt 0)

10+ Year Member



Thank you Jim!

Amazing. I will try to understand the above and modify it to work in case when not only the .tld is changing but the domain and tld.

firstdomain.com redirects to lang=1 seconddname.org redirects to lang=2

What did you mean by /Don't try to use this except for testing until you have changed all links on your site to point to "/" instead of "/index.php"./?

http://www.example.com/index.php?option=domain&view=search&id=4&lang=hu is OK or not?

Thanks again!
Kalman

[edited by: jdMorgan at 2:09 am (utc) on May 15, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:22 am on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



modify it to work in case when not only the .tld is changing but the domain and tld.

The only non-obvious change would be in this rule:


# Externally redirect (only) direct client requests for /index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(\?[^\ ]*)?\ HTTP/
RewriteCond %{HTTP_HOST} (example\.ro¦example2\.com)
RewriteRule ^index\.php$ http://www.%1/ [R=301,L]

Replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe characters.

http://www.example.com/index.php?option=domain&view=search&id=4&lang=hu is OK or not?

It is not "OK," it is just marginally-acceptable. It is a waste of bandwidth and storage, makes the URL ugly, harder to say, to type, and to remember, and is completely unnecessary.

Given a choice between http://www.example.com/index.php and http://www.example.com/, the shorter URL is simpler, prettier, easier to type, and therefore better.

To illustrate, let me ask: Do you go to google.com.ro/index.cgi or to google.com.ro/ ?

Jim

Yatko

5:31 am on May 15, 2009 (gmt 0)

10+ Year Member



All clear and many thanks again!
I need a SEF engine that will give my URL a face lift.
Kalman