6

I have referred other related question but they are not clear to me. Please explain me: How can I make subdomain in PHP?

Example :

http://www.domainname.com/page.php?usname=sundar

I want change the link to this

http://sundar.domainname.com

Is this possible in PHP? Or any other way to do it?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
ananth
  • 71
  • 1
  • 3
  • this is handled by the server (i.e. apache) not php – Ibu Aug 01 '11 at 08:05
  • 1
    Possible duplicate of http://stackoverflow.com/questions/183928 – vascowhite Aug 01 '11 at 08:20
  • Possible duplicate of [How to let PHP to create subdomain automatically for each user?](http://stackoverflow.com/questions/183928/how-to-let-php-to-create-subdomain-automatically-for-each-user) – Kirk Hammett Nov 05 '16 at 10:31

3 Answers3

6

You may create a virtual sub domains and then you can redirect it with .htaccess.

You also have to create

A Record look like this

*.exmple.com A 127.0.0.1 or your IP

in your DNS.

Then add something like this in your .htaccess

RewriteRule ^([aA-zZ])$ home.php?username=$1

RewriteCond %{HTTP_HOST} ^(^.).mywebsite.com
RewriteRule (.
) home.php?username=%1

It should help

Community
  • 1
  • 1
vsuhov
  • 85
  • 1
  • 9
3

my way:
you will have to create a wildcard subdomain and point it you your domain.

http://www.easymodrewrite.com/example-subdomains

Then you can parse $_SERVER['HTTP_HOST'] for querying out the user name .

if you are under hosting plane, you can probably create it from the GUI.
Else, you need to via the APACHE panel and configure the DNS.

fatnjazzy
  • 6,070
  • 12
  • 57
  • 83
0

You could use .htaccess url rewrite to make that.

Just rewrite www.(username).site.com to www.site.com/profile/(username)

the code for that would be something like..

RewriteRule ^(.*).site.nl site.nl/profile/$1

if my memory serves me right

Johan
  • 1,537
  • 1
  • 11
  • 17
  • Sorry -- it does NOT work like that -- `RewriteRule` only works with path part of the URL. To work with other parts (domain name, port, query string) use `RewriteCond`. – LazyOne Aug 01 '11 at 12:09
  • Yeah that does sound better, my bad, I was typing from memory, – Johan Aug 01 '11 at 12:34