Question

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

Was it helpful?

Solution

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>

OTHER TIPS

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);

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top