Who Knows the Code?
January 29, 2008 7:04 AM   Subscribe

Domain Mapping via .htaccess: I want to map a second domain I have to a subdirectory in my main Web directory (BTW, this is not a Typepad blog). My ISP asked that I set up the redirect via my .htaccess file but failed to supply the code to do that (we're having a little chat about suggesting I do something without giving me the tools I need to do it -g).

That being said, I'm familiar with .htaccess (I have one in place with code to deal with certain sites which are stealing my bandwidth). However, I'm not well-versed in creating the code to map my new domain to the subdirectory I've created.

I'm hoping someone in the AskMeFi hive can supply me with the code (bonus points for making sure that both www.mysite.com and mysite.com will go to my subdirectory). Thanks.
posted by Taken Outtacontext to Computers & Internet (22 answers total) 3 users marked this as a favorite
This is something you'll want do to via your conf files not .htaccess

something along the lines of

<virtualhost yourdomain.com:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /path/to/site/subdirectory/

in its simplest form - your apache version will dictate the placement and directives
posted by handybitesize at 7:13 AM on January 29, 2008

Ah on review - unless you have a site root for your second site from your ISP.

In that case you could just symlink it to the correct folder or do something along the lines of

RewriteRule (.*) /path/to/site/subdirectory [L]

although someone is bound to remind me that mod_rewrite is not the correct tool for this job
posted by handybitesize at 7:22 AM on January 29, 2008

I'm not sure I have access to the conf file. So I think it will have to be via .htaccess.

And I'm going to need something definitive since I don't have enough knowledge to problem solve (thanks on that).
posted by Taken Outtacontext at 7:34 AM on January 29, 2008

Ok - some more info needed then

Is your ISP hosting this for you?
Do you already have a directory that serves content for seconddomain.com?
Do you want it to appear seamless or are you happy for a url redirect to happen (ie url stays the same or changes)
posted by handybitesize at 7:40 AM on January 29, 2008

Yes. Exactly.
posted by Taken Outtacontext at 8:43 AM on January 29, 2008

If the ISP has things setup reasonably (i.e. 'AllowOverride FileInfo' in their Apache config for your domain and the "new" domain rooted at the same place as your "old" domain), you can follow this recipe in your .htaccess in the top level of you "old" domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.alice\.com [NC]
RewriteRule ^(.*) http://www.mycurrentdomain.com/alice/$1 [L,R]

I guess it's obvious that you'll need to substitute real info for "alice" (twice) and "mycurrentdomain", but it's best to be explicit, eh? Also, there's definitely room for improvement (e.g. in the regex, external vs internal redirects, etc.), but this should get you started.

Note that this is shamelessly cribbed from RSE's excellent URL Rewriting Guide.
posted by so at 8:45 AM on January 29, 2008

Thanks. Will this work for both www.alice.com and alice.com (without the www)?
posted by Taken Outtacontext at 9:04 AM on January 29, 2008

That rule, as written, will not match alice.com (no www). I'd attempt to rewrite (no pun intended) the rule, but every single time I try to change Rewrite rules, I introduce syntax errors for unclear reasons.

Do you have any subdomains (e.g., forums.alice.com), or is it just alice.com and www.alice.com? (It would make things much easier if the latter.)
posted by fogster at 9:36 AM on January 29, 2008

Oh, a totally inefficient, but totally effective way would be:
RewriteCond %{HTTP_HOST} ^www\.alice\.com [NC]
RewriteRule ^(.*) http://www.mycurrentdomain.com/alice/$1 [L,R]

RewriteCond %{HTTP_HOST} ^alice\.com [NC]
RewriteRule ^(.*) http://www.mycurrentdomain.com/alice/$1 [L,R]
It should be trivial to write a regular expression to match both in one Condition. This way has it as two separate rules, which should work fine, it's just not as elegant as it can be.
posted by fogster at 9:38 AM on January 29, 2008

Do you have any subdomains (e.g., forums.alice.com), or is it just alice.com and www.alice.com? (It would make things much easier if the latter.)

fogster, if you mean does my new domain (the one I want to map to a subdirectory) have any subdomains, the answer is no. Presently my main domain does have subdomains, the difference is that they look like this subdomain.maindomain.com. But in this case I want the "subdomain" to be mapped to an entirely different domain name. Make sense?
posted by Taken Outtacontext at 10:18 AM on January 29, 2008

Well, if you want to match in a single re (an admirable goal, which I missed in your spec), just try this:

RewriteCond %{HTTP_HOST} ^(www\.)?alice.com$

posted by so at 12:23 PM on January 29, 2008

So, would a more economical way of writing it be:
RewriteCond %{HTTP_HOST} ^(www\.)?alice.com$
RewriteRule ^(.*) http://mycurrentdomain.com/alice/$1 [L,R]
Can I get rid of the www. in that last line --does it make a difference? fogster wrote it as:
RewriteRule ^(.*) http://www.mycurrentdomain.com/alice/$1 [L,R]

posted by Taken Outtacontext at 1:27 PM on January 29, 2008

Yes, that is the most-economical in my mind (which says very little).

And yes, it's fine to leave off the 'www.' in line 2.
posted by so at 1:41 PM on January 29, 2008

Wait a minute. You're supposed to instill confidence in me, so. I can understand what these lines do. But I don't know the syntax of unix well enough to write it by myself. So I'm counting on you! Or: So, I'm counting on you! -g
posted by Taken Outtacontext at 1:49 PM on January 29, 2008

Since lots of answers have been offered, by no explanations, and you seem to require understanding (as opposed to blind obedience), I'll add my two cents.

The suggestions that have been made and refined are rules that implementation the apache server module called mod_rewrite

Mod_rewrite's purpose is to rewrite URI's on the fly - so in your instance, when someone requests http://www.alice.com, their request gets rewritten (according to the rule you place in your .htaccess file) to become what you need it to be (in this case http://www.someotherdomain.com/alice/)

And what the "most economical" answer given says (in English) is: all requests that begin with either www.alice.com or just alice.com should be changed (rewritten) into requests for http://www.mycurrentdomain.com/alice/

make sense?

Of course, all of this is moot if your ISP has not enabled mod_rewrite - best to check with them if you have any problems implementing the suggestion above.
posted by namewithoutwords at 4:11 PM on January 29, 2008

Heh, the proof is in the pudding, as they say.

For more info on mod_rewrite, follow the "RSE" link above. That also has pointers to the basic mod_rewrite docs (the former is more of a cookbook type thing).

To understand the regular expression syntax (the right-hand side of the RewriteCond), consult the unix man page "perlre", or O'Reilly's excellent book on the subject.

I'm sorry to offer only these pointers, but those folks do a great job of talking about something I'd only muddle.
posted by so at 8:00 PM on January 29, 2008

Thanks to all of you. I'm going to test this out.

So, I passed on your economical expression to the ISP's techs and they said that should work. In addition, they said that what they normally use in these cases is:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} dev.digi-manage.com
RewriteCond %{REQUEST_URI} !dev/
RewriteRule ^(.*)$ dev/$1 [L]
RewriteCond %{HTTP_HOST} test.digi-manage.com
RewriteCond %{REQUEST_URI} !test/
RewriteRule ^(.*)$ test/$1 [L]
RewriteCond %{HTTP_HOST} kfb.digi-manage.com
RewriteCond %{REQUEST_URI} !kfb/
RewriteRule ^(.*)$ kfb/$1 [L]
First, I'd like to know what the difference is between this expression and the one we came up with here:
RewriteCond %{HTTP_HOST} ^(www\.)?alice.com$

RewriteRule ^(.*) http://mycurrentdomain.com/alice/$1 [L,R]
Second, I'd like to know where in my ISP's Rewrite should I put my newdomain.com and where should I sub my path (maindomain.com/subdirectory)?
posted by Taken Outtacontext at 6:00 AM on January 30, 2008

I'll try this

RewriteEngine On - Switches mod_rewrite on

Options +FollowSymlinks - Instructs the server to follow symbolic links

RewriteBase / - Tells mod_rewrite if the URL and file path are different - their not

Then we have a series of if statements....

RewriteCond %{HTTP_HOST} dev.digi-manage.com
IF the HTTP_HOST EQUALS dev.digi-manage.com...

RewriteCond %{REQUEST_URI} !dev/
AND the URI (the bit after the domain) does NOT EQUAL dev (so you don't get in a loop)

RewriteRule ^(.*)$ dev/$1 [L]
THEN capture the entire URI (.*) and append it to dev so that
dev.digi-manage.com/my/directory/structure becomes dev.digi-manage.com/dev/my/directory/structure

The [L] flag says this the end of the rewriting process for this URL

The suggested query says..

RewriteCond %{HTTP_HOST} ^(www\.)?alice.com$
IF the HTTP_HOST EQUALS alice.com WITH an option www. in front

RewriteRule ^(.*) http://mycurrentdomain.com/alice/$1 [L,R]
THEN capture the entire URI (.*) and append it to http://mycurrentdomain.com/alice/ so that
www.alice.com/my/directory/structure becomes http://mycurrentdomain.com/alice/my/directory/structure

The [L,R] flags are for LAST as above and REDIRECT, which sends a HTTP 302 to the client meaning, amongst other things, that the browser address bar will change to reflect the new URL. I was under the impression that you didn't want this bit....

For you second question you'll want to point alice.com to resolve to maindomain.com and in then you can use (as per your isp example)

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?alice.com$
RewriteCond %{REQUEST_URI} !alice/
RewriteRule ^(.*)$ alice/$1 [L]

In brief - if someone requests a page using alice.com and its not already serving from the alice folder then serve it from the alice folder.

posted by handybitesize at 9:15 AM on January 30, 2008

Thanks. I think I could use "Apache for Dummies." ;-)
posted by Taken Outtacontext at 10:15 AM on January 30, 2008

It worked!!!!!

Thanks to all of you. I'd like to mark all of these as the best answer since it was a great collaborative effort. I really appreciate it.
posted by Taken Outtacontext at 10:36 AM on January 30, 2008

BTW, handybitesize, going back to your first response: I don't have access to the conf file since I'm on a shared, rather than dedicated server. But your last entry really helped.
posted by Taken Outtacontext at 10:39 AM on January 30, 2008

« Older Why is my dog scared of cooked lamb?   |   I can haz eazy cardiology literature search? Newer »
This thread is closed to new comments.