• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

Create a sub domain with .htaccess ???

Hi experts,

I have a web site hosted in a LINUX environment and let’s assume the URL as http://www.abc.com 

Now I need to create a sub domain called http://xyz.abc.com and redirect all the traffic coming to that URL to folder called “xyz” in the root. I'm willing to deploy a application in to "xyz" folder.

Hosting support service has informed me to do it using .htaccess file. Also there is some facility to enter A records, CNAME, etc if needed.

Can you please provide me the necessary guide to this?  

Thanks a lot !
4 Solutions
Hi NetMaxtor,

You can create the third level domain by entering an A record ( IP address of the webserver ) for the xyz domain.   From there, you can use a mod_rewrite rule in an .htacess file to control the redirection ( instead of having apache do it ).

Put this in your .htaccess file:

Options FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^xyz.abc.com$
RewriteRule ^/(.*) http://abc.com/xyz/$1

That should redirect anything http://xyz.abc.com/folder/page.html to http://abc.com/folder/page.html and other variations.
NetMaxtorAuthor Commented:
i have create a A record to point xyz.abc.com to server IP address (say
And there was not a .htaccess file the root folder /www

I have create new file (used vim) and put it in the root, restarted the apachectl, but still xyz.abc.com directing to the home site (abc.com)

Please advice !
Have .htaccess overrides been enabled for abc.com?  Have you checked in the error_log and access_log to see if there is more information available to you about what page is being delivered?
Also, some ISPs offer sub-domain forwarding, so you can setup xyz.abc.com to point to (relative to web root) /xyz.  Check your control panel to see if that option (or an equivalent) is available (I'm guessing the fact that they advised you to use .htaccess means that it doesn't, but I thought it worth mentioning)
...also, I'm not sure if the leading slash is always present in the URI.

Try replacing

   RewriteRule ^/(.*) http://abc.com/xyz/$1

   RewriteRule ^/?(.*) http://abc.com/xyz/$1

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now