homepage Welcome to WebmasterWorld Guest from 54.226.191.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Handling domain and language dependant urls
How to create a rewrite based on the domain and page name
Simon Giddings




msg:4419965
 2:00 pm on Feb 21, 2012 (gmt 0)

My site handles two languages, French and English, with the default being french.
So :
www.example.com = french
en.example.com = english

For SEO handling, URL page names are displayed in the requested language.

I want to be able to correctly rewrite the page name based on the domain AND page requested
I can do this :

RewriteRule ^english-page.php$ ./mypage.php?lang=en [L]

However, this does not allow me to only do the rewrite when the domain = en.example.com, and redirect to a 404 when the domain = www.example.com.

Not being very experienced with htaccess rules, can anyone help me solve this problem ?

 

phranque




msg:4419990
 3:23 pm on Feb 21, 2012 (gmt 0)

meet the RewriteCond directive and HTTP_HOST server variable:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

Simon Giddings




msg:4419991
 3:28 pm on Feb 21, 2012 (gmt 0)

Hi phranque,

So we meet again !
Yep, I looked at this, but have not seen how to use it in this context.
Here is what I tried to do :
RewriteCond %(http_host) ^en\.example\.com [nc]
RewriteRule ^english-page.php$ ./mypage.php?lang=en [L]

As you can probably see straight away, this doesn't work.
Can you help me any further ... please ?

Simon Giddings




msg:4419995
 3:38 pm on Feb 21, 2012 (gmt 0)

Heads up !

Just found what (I think) was wrong - used normal brackets instead of curly brackets in the rewritecond directive.

Is there anything else I should be changing, do you think ?

phranque




msg:4420021
 4:29 pm on Feb 21, 2012 (gmt 0)

in .htaccess i would do something like:
RewriteEngine on
RewriteCond %{http_host} ^en\.example\.com$ [NC]
RewriteRule ^english-page.php$ mypage.php?lang=en [L]

phranque




msg:4420037
 4:53 pm on Feb 21, 2012 (gmt 0)

also, depending on your configuration, you might need this:
Options +FollowSymLinks

lucy24




msg:4420150
 8:39 pm on Feb 21, 2012 (gmt 0)

Just found what (I think) was wrong - used normal brackets instead of curly brackets in the rewritecond directive.

Ouch! You mean in your posted example where you had something in parentheses? I'm impressed it didn't simply crash the server :)

RewriteCond %{http_host} ^en\.example\.com$ [NC]
RewriteRule ^english-page.php$ mypage.php?lang=en [L]

What happens if the user requests

en.example.com/english-page.php?lang=en

Dealing with requests that are supposed to work is the easy part. But if you're rewriting you also have to deal with requests that are not supposed to work. Nothing fancy, just make sure they end up with a 404 rather than a 500 or an infinite-redirect loop.

"It is impossible to make things foolproof, because fools are so ### ingenious." But you need to try.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved