user3116402
user3116402

Reputation: 363

Associate subdomain with PHP variable in the URL

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

Upvotes: 1

Views: 81

Answers (3)

RaviH
RaviH

Reputation: 3584

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.

Upvotes: -1

MathB.Fr
MathB.Fr

Reputation: 292

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>

Upvotes: 2

anurupr
anurupr

Reputation: 2344

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

Upvotes: 2

Related Questions