homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How to direct domains to subfolders using htaccess
cmscss




msg:4228855
 4:45 pm on Nov 10, 2010 (gmt 0)

Hi There,

We have a client with one main site and multiple sub-sites that will use different domains.

Our host has recommended that the secondary domains be set up as domain aliases on the main hosting account and has suggested using mod rewrite to direct the domains to the sub-site folders.

But we don't have much experience with .htaccess files and are unsure where to start.

We're using Expression Engine Multiple Site Manager as the CMS and our main hosting directory is setup like this:

/.htaccess
/index.php
/sub-site-one/index.php
/sub-site-one/.htaccess
/sub-site-two/index.php
/sub-site-two/.htaccess


Here's the .htaccess file currently in the main directory which removes .index.php from the url. It's generated via a Expression Engine plugin - but we can add or remove parts before the plugin writes the file.

RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

# -- NSM .htaccess Generator Start --
# .htaccess generated by NSM .htaccess Generator v1.0.3
# @see: http://expressionengine-addons.com/nsm-htaccess-generator

# secure .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# EE 404 page for missing pages
ErrorDocument 404 /index.php/

# Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp|css|js|flv)$">
ErrorDocument 404 "File Not Found"
</FilesMatch>

# Although highly unlikely, your host may have +FollowSymLinks enabled at the root level, yet disallow its addition in .htaccess; in which case, adding +FollowSymLinks will break your setup (probably a 500 error), so just remove it, and your rules should work fine.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# Remove index.php
# Uses the "include method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(home_page|site_wide|single_pages|galleries|people|news|board|overlays|property-development|development-management|hotel-and-hospitality|funds-management|news|about|members|P[0-9]{2,8}) [NC]
RewriteRule (.*) /index.php/$1 [L]

# -- NSM .htaccess Generator End --



We're not sure where to start so any help or pointers in the right direction would be much appreciated.

Cheers

Ben

 

sublime1




msg:4229725
 8:54 pm on Nov 12, 2010 (gmt 0)

So yes, if the sub site is sub-site-one.mysite.com they you could create a DNS CNAME alias of that to mysite.com -- that just gives the two names the same IP address, and if you are doing this generally, you could do a wildcard CNAME, using *, meaning any subdomain. Convenient, but may not be what you really want.

Anyhoo, once you have that sorted out, you'll need to rewrite the sub-domain to its corresponding directory. I'll assume that foo.example.com really lives in /foo/

A back-reference to a captured pattern in a RewriteCond is a % sign, the back-reference to the pattern from a RewriteRule is a $ sign, so


# Capture the subdomain from the REMOTE_HOST
RewriteCond %{REMOTE_HOST} ^(.*)\.example\.com$ [NC]
# Make sure the path doesn't already start with the subdomain + /
RewriteCond %{REQUEST_URI} !^%1/
# Internally rewrite whatever is in the path ($1) to the subdirectory (%1)
RewriteRule (.*) /%1/$1 [L]


The part that may not work here is that when an internal rewrite occurs in the .htaccess context, it gets re-checked, which is why I put the second RewriteCond in -- second time around, the path would start with foo/, so we would not want to rewrite again. That's a little tricky.

So I really don't know if I have this right, but I think it's close, so please don't take this to the bank. And there's probably a more efficient way to do this.

Tom

cmscss




msg:4230289
 9:53 pm on Nov 14, 2010 (gmt 0)

Hi There,

Thanks heaps for the reply. I'm not sure your answer is what I'm looking for or I'm confused at your response - which is probably more likely.

If I use sub-domains on our host, the sub-site directory will live outside the root and I'm not sure Expression Engine will have access to it without help from the host.

What I would like to do, is to have: "www.main-domain.com/sub-site/"
be rewritten as: "www.sub-site.com"

Is that what your reply would allow me to do? Sorry if I've misunderstood...

Cheers

Ben

sublime1




msg:4230302
 10:30 pm on Nov 14, 2010 (gmt 0)

Ben --

Does the user go to "www.sub-site.com" and see content located in www.main-domain.com/sub-site?

Are there really separate domains, or is sub-site a sub-domain of main-domain. If the sub-site is "foo" is it "www.foo.com" or "foo.main-domain.com"?

Tom

cmscss




msg:4230313
 10:54 pm on Nov 14, 2010 (gmt 0)

OK, me using the term "sub-site" might be causing confusion - that's just how I'm thinking about it because of Expression Engine's multi-site manager.

Let's think of these sites as completely separate sites and so need completely separate domain names.

Are there really separate domains, or is sub-site a sub-domain of main-domain.


They're all separate domain names for products and can't be accessed via sub-domains.


If the sub-site is "foo" is it "www.foo.com" or "foo.main-domain.com"?

Yes it's accessed at (or I would like it to be accessed from) www.foo.com

It lives in a folder on the main hosting account so is currently accessed at:
www.corporate-company-domain.com/foo/

Hope that makes sense...

sublime1




msg:4230329
 12:47 am on Nov 15, 2010 (gmt 0)

Ben --

Makes more sense now.

OK, so two things: first for every domain (foo.com, bar.com...) or their subdomains (www.foo.com, www.bar.com) you'll need to have DNS records set up that point to the same IP address as main-domain.com.

Often, your hosting company provides DNS management tools allowing you to create these records. If you expect many of these, you should check the limits set by your hosting provider. In the end, whether you use "A" records (map a domain name to an IP address) or "CNAME" records (map one domain name to another), you'll be able to resolve the foo.com (and www.foo.com) addresses.

I think the DNS part of things will apply regardless of how you plan to proceed next.

The other part required is a way for your Apache server to sort all of this out. There are two distinct ways possible.

If you have control over this (either directly by configuring Apache on your server) or indirectly (e.g. via cPanel, Plesk, webmin or other tool) you could create a virtual host that looks for a request to a domain name, regardless of it's IP address. This is by far the most direct solution, since each virtual host can have its own document root. It may or may not be the best solution, depending on your system, but, in short, for foo.com you would set a ServerName of foo.com, a ServerAlias of www.foo.com, and a Document Root of something like htdocs/foo, perhaps with an associated Directory container providing special permissions, and so forth. Requests like this would never see your main server's DocumentRoot.

But chances are, this is not what your Multi-site manager is designed to do. I know nothing about it, haven't paid any attention to the various rules you posted, so can only draw on other tools like WordPress and Drupal which both have multi-site management built in.

In these cases, I believe it is assumed that Apache is suitably configured to pass along any request to your main domain's IP. This would suggest (I believe) an IP-based configuration, and you'll need to know if your host has set up your main-domain.com configuration to be specific to the domain name (name-based host) or not (ip-based host). If the former, then you will need to find a way to have your host accept requests from multiple domain names pointing to the same IP address.

If the latter (IP-based hosting), then the rewrites I provided before are close. Here's a revision:



# Capture the domain from the REMOTE_HOST. You probably want the
# second part after an optional www up to the next . (handling .com,
# .co.uk, .biz, etc.)
RewriteCond %{REMOTE_HOST} ^(www\.)?(.*)\. [NC]
# Make sure the path doesn't already start with the domain + . some
# characters, then a /
RewriteCond %{REQUEST_URI} !^%1\.*/
# Internally rewrite whatever is in the path ($1) to the subdirectory (%1)
RewriteRule (.*) /%1/$1 [L]


Simple, eh? (Again, disclaimer -- not sure if this works, and more importantly: whatever tool you're using to do multi-site most likely has this all worked out and whatever we do here is likely subverting their complete, elegant, and well-considered solution. I would check with them if you get this far.

Tom

cmscss




msg:4236550
 3:42 am on Nov 30, 2010 (gmt 0)

Mate, thank you so much for your reply.

I pasted in your code but it didn't work and to be honest, I'm completely lost so that's probably why!

I have A records setup for the following - all pointing to the server's IP address:
www.domain-one.com
domain-one.com
www.domain-two.com
domain-two.com


The hosting account is set up as www.domain-one.com and a domain alias is setup in Plesk for www.domain-two.com (I can't setup virtual hosts using this hosting account - only domain aliases)

When I visit www.domain-one.com I see the first website - everything works sweet.
When I visit www.domain-two.com I see the first website again - which is to be expected at this point.

So based on your .htaccess example above, I don't understand how the system knows to direct www.domain-two.com to the directory that houses the second website? Where do I put that info in - and do I need things like "RewriteEngine On" etc?

Is it easier to do this if I set up all the other sites using sub-domains maybe?

Sorry this is painful, any help would be much appreciated.

Cheers

Ben

cmscss




msg:4237048
 8:33 pm on Nov 30, 2010 (gmt 0)

I guess I need to do something similar to this:

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain-one.com
ServerAlias domain-one.com
DocumentRoot /usr/local/www/vhosts/domain-one.com/httpdocs/
</VirtualHost>

<VirtualHost *:80>
ServerName www.domain-two.com
ServerAlias domain-two.com
DocumentRoot /usr/local/www/vhosts/domain-one.com/httpdocs/domain-two/
</VirtualHost>


But do it in a .htaccess file because my host won't give me access to the httpd.conf

Anyway, thanks for the replies and your patience - I just can't find info (probably because I'm not searching for the right terms) on how to do this using a .htaccess file

cmscss




msg:4237558
 8:47 pm on Dec 1, 2010 (gmt 0)

Sorry to bump this, going live on Friday!

Is this even possible via .htaccess?

sublime1




msg:4237573
 9:26 pm on Dec 1, 2010 (gmt 0)

NameVirtualHost and VirtualHost are not allowed in .htaccess. Most shared hosting services don't. For as little as $20/month you can get real VPS hosting.

If the software you're using does not provide built-in support for multi-site, and without a pretty solid knowledge of how to configure Apache, I can only really say I am not sure what else to suggest except to get all warm and cozy with the Apache documentation, especially around rewriting.

Sorry.

cmscss




msg:4237628
 10:59 pm on Dec 1, 2010 (gmt 0)

Thanks Tom.

We have a Media Temple account but now use hosting based here in New Zealand because we've had so many issue with their service - websites going down, security problems etc.

But NZ hosts don't seem to offer virtual hosting unless you look after the full server - which is out of my league.

Does anyone have any US based hosts they could recommend - bear in mind I'm a designer so need an easy control panel!

Any suggestions welcome.

Oh, and thanks heaps for your suggestions Tom.

Cheers

Ben

sublime1




msg:4237647
 11:31 pm on Dec 1, 2010 (gmt 0)

It's funny you should ask -- I use a great service called RimuHosting, which I believe is based in NZ -- they offer outstanding support, reliability and in the US, a $20/month VPS server (virtual private server). They have hosting centers in various locations including one in Texas. They have a nice UI for server management (webmin). It's definitely geekier than the plug-n-play shared hosting service. I can't recommend them highly enough.

Tom

cmscss




msg:4237652
 11:40 pm on Dec 1, 2010 (gmt 0)

Thanks mate - yes Rimu is a native NZ plant. Will check them out - thanks!

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