1

I manage a website with different country versions accessable by a php variable. Example for UK:

http://autocosts.org/index.php?c=UK

How do I associate the subdomain http://uk.autocosts.org to http://autocosts.org/index.php?c=UK

I'm using Hostgator severs

3 Answers 3

2

It's possible with URL Rewriting.

Put this in a .htaccess file in you website root folder :

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.autocosts\.org$ [NC]
RewriteRule (.*)  http://autocosts.org/index.php?c=%1 [L,QSA]
</IfModule>
2
2

The domain is stored in the HTTP_HOST index of $_SERVER You can try this

 $url = "http://autocosts.org/index.php?c=".
 $country =  substr($_SERVER['HTTP_HOST'], 0,2);
 $url .= strtoupper($country);
3
  • if you want to redirect the user to that url you can use header("Location: ".$url);
    – anurupr
    Commented Feb 14, 2014 at 14:03
  • thank you, though this solution will create a paradox, for example this might be possible: de.autocosts.org/index.php?c=PT Commented Feb 14, 2014 at 14:31
  • that url is possible . but if the code is in index.php it will always s redirect to http://autocosts.org/index.php?c=DE
    – anurupr
    Commented Feb 14, 2014 at 14:32
-1

You get get this effect by doing an internal redirect of the requests matching uk.autocosts.org.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^uk.autocosts.org$
RewriteRule ^.*$ %{REQUEST_URI}?c=UK [PT]

However, you have to be careful with the GET URLs having a query string. You need special handling if you have GET URLs with query string.

3
  • Yes, that is what the question is about - read it carefully. It does exactly what is needed. What is the problem with that?
    – RaviH
    Commented Feb 14, 2014 at 14:06
  • I quote the user. I manage a website with different country versions accessable by a php variable. Example for UK. So what he is trying to say is that he has different domains for the same website. So for example users from UK can access the website using uk.autocosts.org, people from india can access the website using in.autocosts.organd so on. So your solution would work only for the domain uk.autocosts.org and applying the same solution for other domains would prove unsuccessful.
    – anurupr
    Commented Feb 14, 2014 at 14:10
  • Ok - I agree with you. Extending the solution to that is trivial. Now there is no point in repeating it since other answers have proposed that too already.
    – RaviH
    Commented Feb 14, 2014 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.