Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Rewriting Subdomains with .htaccess

What's wrong with my code?

7:03 pm on Sep 26, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 30, 2002
votes: 0


I want to rewrite my subdomain to appear as my top level domain.

I don't want a redirect. I just want a link that is currently seen as [sub.domain.com...] to actually be seen as [domain.com...] or [domain.com...] when a page prints out.

Why is my code not working?

RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.sub.domain.com$
RewriteRule ^(.*)$ [domain.com...]


1:49 am on Sept 27, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0


Something is missing from this equation. Given that this .htaccess file is located in the web root of your site, then it can and will do nothing. That's because in order for the RewriteRule to be processed, the request has to be delivered to the directory where the .htaccess code resides. If that is true, then you don't need to do anything, i.e. you don't need mod_rewrite code for this, because you are already in the correct directory.

Also, there are two forms to a RewriteRule. One is a redirect and uses the basic form:

RewriteRule ^somepath$ http://somedomain [R,L]

This can be either a 301 or 302 redirect. It defaults (as shown) to a 302. Use [R=301,L] to force a 301-Moved Permanently, and [R=302,L] for a 302-Moved Temporarily if you like to have your code "self-documenting" for later reference.

The second form is something like this:

RewriteRule ^somepath$ /someotherpath [L]

This is not a redirect, it is simply a path substitution. Note that the substitution path is local - a URL beginning with [somedomain...] cannot be used.

Now, if you make your RewriteRule compliant with the requirements of the second form -- no [somedomain,...] it may be clearer why your rule appears to not work:

RewriteCond %{HTTP_HOST} ^(www¦sub)\.domain\.com$
RewriteRule ^(.*)$ / [L]

This will take a request for www.domain.com or sub.domain.com, and serve the index file of the current directory, which I suspect looks like it does nothing. Note that it will only work if either of the domains is requested without the trailing slash because you have end-anchored the pattern with "$".

After reviewing the above, you might want to re-state what you are trying to do. Because of the inconsistencies noted above, I can't really tell. It all depends on what directory the subdomain requests are delivered to now, and where you want to deliver them to using mod_rewrite. In all cases, the .htaccess file must be located in the directory where the visitor initially "lands" to "send" the visitor to the target directory.

Also, if you want to show a different domain name in the user's address bar, then that requires an external redirect.


<added>Note that I have used (www¦sub) to compress your two RewriteConds into one. If you wish to use this trick, you will have to edit the "¦" character to change it to a solid vertical pipe character from your keyboard. Posting on this board changes them to broken vertical pipes, which will not work in mod_rewrite.</added>

4:17 pm on Sept 27, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 30, 2002
votes: 0

Hi Jim,

Thanks for the reply. That gives me a much better understanding of mod_rewrite. I probably read Apache's tutorial 10 times and you made it much more clear. Let me explain my exact problem.

My web host allows me to create "Add On" domains. When doing this it adds an entry to httpd.conf that points my new domain (a different website) to a subdomain of the main website.

ex) newdomain.com points to subdomain.mainwebsite.com

It also creates a folder in the root directory (of the main website) that becomes the root directory of my new domain. I've added my .htaccess file here.

Where I run into problems is that I am running some .pl scripts out of the new cgi-bin (I can't edit these, I bought them). These scripts reference URLs in my new site but they are referenced as


Is this a setting on my hosts server that is causing this? Or can I use .htaccess to rewrite the subdomain reference with my new domain?



1:28 am on Oct 28, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Sept 29, 2002
votes: 0

There are two solutions that I can see. The following example assumes your username is mydomain and the subdomain is called subdomain!

1) If you have shell access, you can go to your main directory:
cd /home/mydomain/public_html

You should see the directory for your subdomain listed
delete it:
rm -r subdomain

now create a symbolic link from your subdomain TO your main directory:

ln -s /home/mydomain/public_html subdomain

It will now work

OR if you do not have shell access ask your host to edit httpd.conf and change the DocumentRoot of the subdomain from /home/mydomain/public_html/subdomain to /home/mydomain/public_html

Hope this helps!


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members