views:

36

answers:

2

Domain based redirect to different PHP files via htaccess

Hello there. Here's the thing: I have two domains here [http://www.myproject.com and [http://www.myproject.com.br .. In my root folder i have all the stuff for my projects including two "indexes": "index-en.php" for english and "index.php" for portuguese.

Can i have some trick in htaccess that redirect my users to one of these files depending on domain? .. something like:

[http://www.myproject.com [OR] [http://myproject.com redirect to [http://www.myproject.com/index-en.php

and

[http://www.myproject.com.br [OR] [http://myproject.com.br redirect to [http://www.myproject.com.br/index.php

??

Sorry if this is a stupid question but im almost crazy looking for tutorials over the internet and i cant get something that works or some response if this is possible or just a stupid question .. Can someone please give me some direction? Maybe some solution with a conditional directoryIndex (I dont know if that's possible).

thanks a lot

Actually i have this in my .htaccess:

ErrorDocument 404 /404.php

<IfModule mod_rewrite.c>
  #Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

  RewriteCond %{HTTP_HOST} ^myproject.com$ [NC]
  RewriteRule ^(.*)$ http://www.myproject.com/index-en.php [R=301,L]

  RewriteCond %{HTTP_HOST} ^myproject.com.br$ [NC]
  RewriteRule ^(.*)$ http://www.myproject.com.br/ [R=301,L]
</IfModule>

This just does the "non www to www" - that works correctly

A: 
RewriteCond %{HTTP_HOST}  myproject\.com$
RewriteRule ^/$  /index-en.php

RewriteCond %{HTTP_HOST}  myproject\.com\.br$
RewriteRule ^/$  /index.php
Ian Wetherbee
Hello, thanks for answering.That didnt seems to work.. i removed my actual RewriteConds and Rules and put yours but nothing seems to change (The "non www to www" does'nt work anymore)
Fabio
A: 

This should do what you want. The RewriteCond trickery is probably a bit overkill here, but it does make adding extra languages easier:

ErrorDocument 404 /404.php

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}/index.php    \.com\.br/(index.php)$ [NC,OR]
RewriteCond %{HTTP_HOST}/index-en.php \.com/(index-en.php)$  [NC]
RewriteRule ^ %1
Tim Stone
THanks man! That did the trick perfectly! Thanks a lot!
Fabio
Me again =|I've just noticed that my "ErrorDocument 404 /404.php" isn't working anymore ... it is possible to fix this? Thanks a lot again anyway.
Fabio
@Fabio - Ah, whoops. You might want to try changing `RewriteRule ^ %1` to `RewriteRule ^$ %1`, and see if that fixes things.
Tim Stone
@Tim - Yes! (laughing) That fixed it!I'll post something about your help in my blog to thank you. You really saved me here man.
Fabio
@Fabio - Awesome, and cool! I'm just glad to help. :)
Tim Stone
Here's the post http://www.paespedro.com.br/blog/mercado/domain-based-redirect-to-different-php-files-via-htaccess. Sorry it's in Portuguese, i'm just saying that im happy that we have people just like you and others here that really helps who's in need. That's it.
Fabio