It is rather neat -once you have your own domain to have some subdomains for different purposes. A usefull subdomain might for example be downloads.example.com. Any user will understand what one can expect at such a domain.

Things like this can be done with drupal 4.3 in the core or in 4.2 with the path module. In that case you can point your users to example.com/downloads and this page drupal will rewrite towards the download page. The way of rewriting these domains is rather neat, but having subdomains might in certain case have a better image or be more usefull.

The site drupal.org uses the shortcut hostname.example.com as well. For example to the documentation place, http://documentation.drupal.org leads to http://www.drupal.org/node.php?id=253 (mhhh, we should use the new clean url here!).

So how can you use this service? What follows is a short howto.

    Say you own the domain example.com (rfc2606);
    Say you have a drupal page example.com/node/view/nid
    You want to have a shortcut to this page labelled nid.example.com

Now you have to do the following things:

1. Edit your DNS

2. Edit your .htaccess

Regarding 1), editing your DNS

I wont explain how DNS works, but if you have basic knowledge of DNS and adminster one or more domains, you should be able to add a hostname in your zone called nid(.example.com.). Please make sure you update the TTL.

Another way of doing this is by adding a wildcard in your domain. This wildcard (*) makes that any hostname in your domain will automatically be resolvable. Now wildcards for toplevel domains might be considered very bad (see the rants against verisigns sitefinder service), I think having one in a domain is not *that* bad.


Note that there is one very big disadvantage of having all hostnames resolved. Any word (and i do mean any word) will point to your site, if you like the word or not. Together with the standard drupal behavior of not generating an error pages (e.g. a 404 page), this can lead towards very undesired results. For example http://i-think.drupal.org/rocks will give a valid page, as well as less flatering ones...

Regarding 2) edit hour .htaccess

Once you edited your zone with the new hostname and you tested from various places that this hostname resolves towards the right IP adress, you can edit your .htaccess file in the root of your webserver. This assusmes you are using apache.

First make sure that you have mod_rewrite enabled in your apache webserver. There are more ways to make sure you have mod_rewrite as a module enabled in your webserver, an easy way is to make a small php file somewhere in your document root with
<? phpinfo() ?>

Now surf towards this php file and search for mod_rewrite in the page. If this module is not enabled, ask your hosting party or download the module your self and enable it.

Once you have mod_rewrite installed we can edit the .htaccess file in the root of your webdirectory. Start your favourite editor and check if the line
RewriteEngine On
is there. If not, add the line.

Now add the folowing entrys:

RewriteCond %{HTTP_HOST} ^nid\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/node/view/nid [R=301,L]

Surf towards your new subdomain nid.example.com and see if you are going to example.com/node/view/nid. Check the error log in case things fail, you dont even have to restart the webserver.

Having shortcuts this way can be very handy for taxonomy

Hope you enjoy your new shortcut! Have fun.

Comments

NaSPA1’s picture

Thanks to this how-to, I should be able to implement this facility in my fledgling site.

Radi

Gunny-1’s picture

first configured the subdomain successfully then
modified the the .htaccess file particulary rewrite directive,

<IfModule mod_rewrite.c>
  RewriteEngine on

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

  RewriteCond %{HTTP_HOST} ^myname\.sitename\.org$ [NC]
  RewriteRule ^(.*)$ http://sitename.org/blog/5 [R=301,L]

</IfModule>

myname.sitename.org is the subdomain and sitename.org is the main domain. when i navigate to the subdomain i just get, "you probably have not replaced the index.html file." what is going wrong here.

bertboerland’s picture

sounds like a plesk error to me. check with your hosting party, maybe they can help you here.

--
groets
bertb

--
groets
bert boerland

liban’s picture

Here is what my .htaccess file looks like:


RewriteEngine on

RewriteCond %{HTTP_HOST} ^nid\.exemple\.com$ [NC]
RewriteRule ^(.*)$ http://exemple.com/nid [R=301,L]

# Rewrite current-style URLs of the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The DNS configuration is working. But no matter what subdomain I request , I land on the front page. I have exhausted all the possibilities I could think of. Please help ...

P.S. Could pathauto have any insidence? I'm currently using it to change example.com/blog/1 to example.com/username

Thanks

kajamohideen’s picture

Hello,

I don't know anything about DNS or .htaccess. Can you please explain what are all the things I have to do for getting my need fulfilled?

Now my users' blog url is like http://www.mysite.com/blog/x

I want to have the blog URL as http://blogs.mysite.com/x

(where x is the user id)

If possible, is there anyway to use username here. I have installed "'me' alias" module. But, it is NOT working.. :-((

R. Kaja Mohideen

czarphanguye’s picture

Thanks for taking your time to explain this process. I've often wondered how to use sub domains without installing a whole new copy of Drupal into the 'new' folder.

Regards,

Czar

drupalina’s picture

subscribing