views:

288

answers:

3

I'm interested to create subdomains on the fly directly from php. For example when an user create a new page I want that page to be newpage.mydomain.com. Is that possible without changing the php or apache configuration files (supposing I'm using a shared hosting account).

Later Edit: I'm talking about my domain, and I have full access to the domain administration area.

A: 
  1. Set up an error document in your .htaccess file that redirects every single 404 to a file called maybe redirection.php. This .php file is what will handle the "on the fly" redirects.

  2. Add a wildcard DNS record in your zone files, so that [whateverhere].yourdomain.com points to the IP of your webserver

  3. Add a wildcard serveralias in your apache configs by using: ServerAlias *.yourdomain.com

  4. Write the following code in your redirection.php file.

.

<?php

$url = $_SERVER["REQUEST_URL"];
$newurl=str_replace(".yourdomain.com","",$url);
$newcomplete="http://yourdomain.com/".$newurl;
Header("Location: ".$newcomplete);

?>

Does this help a bit?

Urda
This may work on shared hosting, if your provider will allow you to setup a "catch all" for your webserver. Contact the hosting provider.
Urda
The redirection is really required? I can directly analyze the request url and build the page accordingly I assume.
php html
Well then you can take that approach, but you would still need a wildcard DNS entry, and apache needs to know how to respond to the fake subdomain name.
Urda
A: 

Yes you can do this. You need to make sure that first you set up a wildcard * A record against your domain in your domain register DN panel.

Once you have set up the wildcard, you can just look at the >

 $_SERVER["SERVER_NAME"]
Laykes
A: 

it's not possible through php alone. and I don't think shared hosting allow that. anyway, for that you have to own a domain (or have permission to edit DNS record) then you can add wildcard record to allow any subdomain to point to a single machine (identified by its IP adress)

edit (from Powerlord comment)

apache have to redirect each subdomain to the same vhost usually with ServerAlias *.example.com in a vhost configuration

/edit

then in php you can check which from subdomain the page is request by parsing(spliting) $_SERVER['HOST_NAME']

eg:

$host = explode ('.', $_SERVER['HOST_NAME']);
array_pop ($host);
array_pop ($host);
$subdomain = join ('.', $host);
mathroc
This is wrong. I do exactly what the OP is requesting on THOUSANDS of pages.
Laykes
@Laykes: It really depends on how your host has things configured. By default, Apache (which the OP is asking about) does **not** let you do this; instead it falls back to the `_default_` virtual host, and failing that, the main server configuration.
R. Bemrose
how is that wrong? I explain, which right you need to do that and express that >I< don't think there is a shared hosting service which allow that.does it deserve a -1? maybe a better comment would have been to link to a hosting service that provide that.
mathroc