homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

.htaccess for two domains
one redirecting to a directory, the other to a URL

10+ Year Member

Msg#: 105 posted 1:31 pm on Jan 30, 2003 (gmt 0)

Hi everyone,

I'm having some problems with my .htaccess. Admittedly, I've only just gotten my feet wet, so please bear with me :)

Under a single IP number (Virtual Server) I have, aside from my company domain name, two domains for a project I'm working on: site.com and thesite.com
The content for these domains resides in the directory myDirectories/site/

Since I have one IP, a DNS solution would seem to be non-existent, and I don't have access (or knowledge, for that matter) to httpd.conf...

Using .htaccess, I have no problem redirecting my URLs:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /

RewriteCond %{HTTP_HOST} ^(.*)site.com$
RewriteCond %{REQUEST_URI}!myDirectories/site/
RewriteRule ^(.*)$ myDirectories/site/$1

The thing is though, for promotional purposes, I only want my domain SITE.COM to be used, the other domain is just a fallback-thing, and when users type [THESITE.COM...] or [THESITE.COM,...] I want to have the browser redirect to [SITE.COM...]

I could do this using either a http-equiv refresh or javascript (top.location.replace), but
A. That's not exactly the slickest way to go;
B. Even to do that, my current Rewrite doesn't work like I'd want it to.

Ultimately, I'm looking for a Rewrite that redirects *.SITE.COM to myDirectories/site/, and at the same time directs *.THESITE.COM to [SITE.COM....] Can that be done using .htaccess?

If not, I would be okay using http-equiv refresh or javascript, but then I would like to modify my Rewrite so that ^(.*)site.com$ would redirect to myDirectories/site/, and ^(.*)thesite.com$ would redirect to myDirectories/thesite/
The way it is now, however, I can't get the two domains go to different directories, as my RewriteCond leaves for to much interpretation (it will work for *site.com AND *thesite.com, and adding a separate RewriteCond for *thesite.com won't work, even if I put it first).

I already checked out similar threads on this forum, and though there seems to be a lot of information on problems similar to mine (particularly [webmasterworld.com...] I think), but quite frankly I'm not yet experienced enough in this material to work it out :(



WebmasterWorld Administrator brett_tabke us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 105 posted 2:46 pm on Jan 30, 2003 (gmt 0)
First, welcome to the board.

Try something like this: (put above your other rewrites):

RewriteCond %{HTTP_HOST} ^www.webmasterworld\.org
RewriteRule ^(.+) http://www.webmasterworld.com/$1 [L,R=301]

So it redirects, and the browser rerequests at the new url, which in turn triggers your next rewrites.

Try it - just replace the .com with .org in your address bar now and see how it transparently keeps the old url and just swaps you back out to the .com.


WebmasterWorld Senior Member 10+ Year Member

Msg#: 105 posted 2:53 pm on Jan 30, 2003 (gmt 0)

Welcome to WebmasterWorld [webmasterworld.com] Jello.

Be sure to read Marcia`s WebmasterWorld Welcome and Guide to the Basics [webmasterworld.com] post.

If your code is working fine so far then all you need to do is cause an external rewrite to [site.tld...] when either [thesite.tld...] or [thesite.tld...] are requested. Check for those HTTP_HOSTS ((www\.)?thesite.tld will work as a pattern) and (externally) redirect them to the desired URI. These rules need to be executed before your current code.

I suggest you read An Introduction to Redirecting URLs on an Apache Server [webmasterworld.com]. The URL Rewriting Guide is helpful as well.


<added>Brett was faster and even supplied code :( which I tried to avoid ;)</added>


10+ Year Member

Msg#: 105 posted 9:32 pm on Jan 30, 2003 (gmt 0)

Thanks so much for helping out, it's highly appreciated. Andreas, I totally get what 'trying to avoid supplying the code' ('Give a man a fish...') but Brett, I'm glad you did! And geez, I've never seen anyone on any board have that many posts to his name!
Andreas, thanks for commenting on the code, I'm an eager learner, the links are great too!

Try it - just replace the .com with .org in your address bar now and see how it transparently keeps the old url and just swaps you back out to the .com.

I've modified my code into the following, and it works like a charm! One question though: after the second rewrite, the URL in the browser's address bar becomes: [site.tld...]
What can be altered so that it shows just the domain (without 'index.html')?

RewriteEngine On
Options +FollowSymlinks
RewriteBase /

# THEDOMAIN.TLD [domain.tld...]
RewriteCond %{HTTP_HOST} ^(www\.)?thedomain\.tld
RewriteRule ^(.+) [domain.tld...] [L,R=301]

# DOMAIN.TLD root/directory/domain/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld
RewriteCond %{REQUEST_URI}!directory/domain/
RewriteRule ^(.*)$ directory/domain/$1

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved