Forum Moderators: phranque

Message Too Old, No Replies

htaccess multiple domains on one host

         

bwwd2011

4:41 pm on Jan 8, 2011 (gmt 0)

10+ Year Member



I have one hosting account with several domains

I have a main site in the root of the hosting

I have another site contained within a sub folder

I need to know how I set up htaccess to redirect all domains except one to the main root site to one main domain

and one other domain to the site contained within the sub folder

So that the domain for the site in the sub folder acts right the site works as it should and all the other domains redirecting to one main domain that is using the root of the host

Hope I explained it correctly

I cannot get it to do anything right for some reason

Can anyone point me to a tutorial or some sample code?

Thanks in advance

jdMorgan

4:57 pm on Jan 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post the code you've tried (and specify what file(s) you put it into) as a basis for discussion.

That sentence hints at two important factors -- What code is used, and where it is located. Code for the 'desired' subdomain will need to be placed in the subdomain's file subdirectory, while code for the 'desired' main domain --and presumably for all other 'undesired' domains-- will need to be placed in the main domain's top-level directory.

Jim

bwwd2011

5:11 pm on Jan 9, 2011 (gmt 0)

10+ Year Member



I currently have some standard htaccess code for redirecting all domains that dont match the main one, to the main one and thats how it is set up in the root folder .htaccess


RewriteCond %{HTTP_HOST} !^www.maindomain.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) [maindomain.com%{REQUEST_URI}...] [L,R=301]

Some code here for the main site stuff as needed

But what I need is to modify this so that if its one of the main domain's alternatives it redirects to the main domain, uses the htaccess code from within the root .htaccess file

If however it is one specific new domain I have newdomain.com it pulls all the info from a folder within the hosting and treats that folder as the root of that domain, using a htaccess in that folder and stuff

If that makes sense

I am used to using godaddy which addon domains are a simple type the domain and which folder it points to and your done it treats that folder as if it were root for that domain somedomain.com/index.html would be in root/somedomain/ etc

But with the hosting I am working on at the moment they dont offer such options and tell me I have to do it myself with htaccess or other redirects

jdMorgan

2:26 am on Jan 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unless you're willing to put some effort into trying to code a solution yourself using the resources in our Library and those cited in our Charter, it's going to be difficult for you to set up and maintain this server configuration, and to modify it as your needs grow and change. So all I can do is to present a "generic" solution and hope that it works for you. Unfortunately, if it does not, you will likely lack the vocabulary, knowledge, and/or experience needed to get it working -- something that's difficult or impossible to do without some experience on both ends of a forum conversation. So I offer this without obligation to try to support it:

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
#
# Externally redirect to canonical www.maindomain.com hostname,
# removing optionally-appended FQDN token and port number
RewriteCond %{HTTP_HOST} ^([^.]+\.)*maindomain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301,L]
#
# Externally redirect to canonical www.newdomain.com hostname,
# removing optionally-appended FQDN token and port number
RewriteCond %{HTTP_HOST} ^([^.]+\.)*newdomain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
#
# Externally redirect to canonical otherdomain.net hostname,
# removing optionally-appended FQDN token and port number
# (Note that this hostname is canonicalized to the NON-www variant)
RewriteCond %{HTTP_HOST} ^([^.]+\.)*otherdomain\.net [NC]
RewriteCond %{HTTP_HOST} !^otherdomain\.net$
RewriteRule ^(.*)$ http://otherdomain.net/$1 [R=301,L]
#
# Internally rewrite requests for all but canonical main domain (or blank hostname) to hostname-
# based subdirectory under "addons" subdirectory, unless we've already done this rewrite
RewriteCond $1 !^addons/
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$
RewriteCond %{HTTP_HOST} ^((www\.)?[^.]+\.([a-z]{2,6}|co\.[a-z]{2})$
RewriteRule ^(.*)$ /addons/%1/$1 [L]

The /addons subdirectory level is needed as an easy-to-detect token to prevent an 'infinite' rewriting loop. Further, it serves to preclude the necessity of specifically enumerating all add-on domains in the rewriting rule, and also helps to prevent 'collisions' between add-on-domain subdirectory names and 'real' subdirectories needed by maindomain.com

I picked the name "addons" for this subdirectory to make it quite obvious what it is used for. However, you can name it anything you like, except for the well-known "/w3c" directory; Unless you incorrectly implement future coding, this path-name will only be used internally, will never be 'published' as a URL, and so will remain invisible to clients.

Directory and file structure implied by this code:
maindomain/.htaccess (this code)
other maindomain files
/addons/ subdirectory containing all add-on domains' directories
/addons/www.newdomain.com/ subdirectory
/addons/www.newdomain.com/files
/addons/otherdomain.net/ subdirectory
/addons/otherdomain.net/files

So there's an example to get you started...

I use almost exactly this same approach to implement mobile, test, and development hosts for several "sites."

Having set this up once, you may find (as I have) that this method actually offers a lot more control and flexibility than a fixed, hard-coded Control-Panel-based solution does. Among other things, you'll find it a lot easier to share images, stylesheets, and server- and client-side scripts between your co-hosted domains/hostnames.

Jim

bwwd2011

4:57 am on Jan 16, 2011 (gmt 0)

10+ Year Member



Have been trying this code you sent but every time I do it gives me a 500 server error

Any suggestions, all I did was input my main domain new domain etc and did not modify any other part of the code.

g1smd

4:59 pm on Jan 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What exactly is in the error log?

jdMorgan

5:15 pm on Jan 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, please examine your server *error log* and post the error message(s) you see there. Otherwise it's down to 'debugging by staring at code' -- not the best use of our time.

The problem could be a simple typo (yours or mine) or it could be a directive not allowed on your server, or it could be something else -- The easy way to find out is to see exactly what your server is telling you the problem is in it's error log file.

Jim

bwwd2011

5:30 pm on Jan 16, 2011 (gmt 0)

10+ Year Member



I think I figured it out I removed the \ you had in the domain names before each . and it works just how I want it to at the moment for the most part

Thanks :-)

g1smd

5:35 pm on Jan 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The \ before each . is required.

It says there is a literal dot there, rather than "any single character".

bwwd2011

6:49 pm on Jan 16, 2011 (gmt 0)

10+ Year Member



Well the way I have it works just fine and has no errors

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^www.maindomain.com
RewriteCond %{HTTP_HOST} !^www.newdomain.com
RewriteRule ^(.*) [maindomain.com%{REQUEST_URI}...] [L,R=301]

RewriteCond $1 !^addons/
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteCond %{HTTP_HOST} ^www.newdomain.com$
RewriteRule ^(.*)$ /addons/www.newdomain.com/$1 [L]

g1smd

7:49 pm on Jan 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com
RewriteRule ^(.*) http://www.example.com%{REQUEST_URI} [L,R=301]


simplifies to

RewriteCond %{HTTP_HOST} !^www\.(example|newdomain)\.com
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]


or to

RewriteCond %{HTTP_HOST} !^www\.(example|newdomain)\.com
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]


The above then followed by

RewriteCond $1 !^addons/
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$
RewriteCond %{HTTP_HOST} ^www\.newdomain\.com$
RewriteRule ^(.*)$ /addons/www.newdomain.com/$1 [L]

bwwd2011

8:33 pm on Jan 16, 2011 (gmt 0)

10+ Year Member



Thanks

jdMorgan

9:33 pm on Jan 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the server is accessible via HTTP/1.0, the code will need to accomodate blank HTTP_HOST in order to prevent an infinite redirection loop:

RewriteCond %{HTTP_HOST} !^(www\.(example|newdomain)\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Jim

g1smd

11:08 pm on Jan 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Good call. I use that myself, but forgot to add it here.

modorius

11:45 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



I'm using this method for my three domains on one host, and it's working perfectly.

Only side effect is that when entered to address bar, url: "www.maindomain.com/addons/www.seconddomain.com" leads to second addon domain I have, but url won't change to "www.seconddomain.com". Since there is three domains, is there a simple way to close this "backdoor" for each domain/case, or must I rewrite the other two options out, for each domain/htaccess?

Or is this even a real issue to worry about, concerning duplicate content and search engines?

jdMorgan

1:58 am on Feb 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{THE_REQUEST} ^[A-Z]+\ /addons/[^\ ]+\ HTTP/
RewriteRule ^addons/([^/]+)(/(.*))?$ http://$1/$3 [R=301,L]

Jim

modorius

7:24 pm on Feb 24, 2011 (gmt 0)

10+ Year Member



Yea, this works! Thanks Jim.

modorius

9:42 pm on Feb 24, 2011 (gmt 0)

10+ Year Member



One more strange thing. Everything works as I said, the correct url will appear in the browsers address bar:

If I enter www.maindomain.com/addons/www.seconddomain.com it will lead to www.seconddomain.com (also with this in the address bar), but as I navigate through the pages of seconddomain, and THEN start to type url "www.maindomain. ..." etc in the address bar, the browser starts to auto-complete those urls to the form it has stored all the second domains pages, and that form somehow is "www.maindomain.com/addons/seconddomain.com/page1.shtml" etc. Why does the browser store the pages of second domain this way? Any ideas how to fix this? This was totally unexpected, and of course completely destroys the illusion of separate sites on the same server...

modorius

11:37 pm on Feb 24, 2011 (gmt 0)

10+ Year Member



Additional info: This happens with Firefox, not with IE.

When navigated through pages of www.seconddomain.com, Firefox seems to store one visited page as 3 different addresses on the history.

1. www.seconddomain.com/page1
2. www.seconddomain.com/page1.shtml
3. www.maindomain.com/addons/www.seconddomain.com/page1

All are the same page, number 1 in url, but thanks to autocomplete in firefox, this reveals too much information to user. For me this seems like a problem in my maindomain's htaccess (which is the only one btw). Is this a browser problem or are the lines in htaccess in the wrong order? I also wonder does this cause multiple content for search engines? Any ideas what can be done?

Sorry about the messy explaining, I hope someone can make something out of this, I only managed to broke something else while I "corrected" this one.

g1smd

11:42 pm on Feb 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is returning stuff that YOU have already visited, whether or not those URLs are still valid or not.

However, this exposes another bit of functionality that every site needs: a 301 redirect from old to new URL so that if someone does click an old stored browser bookmark or browser history entry the URL is "corrected" for them and the content is finally served "at" the new URL.

modorius

12:24 am on Feb 25, 2011 (gmt 0)

10+ Year Member



So this is expected behavior? Even if I clear browser cache/history and enter www.seconddomain.com (on address bar), right away there is 3 different addresses for the same page in the history. In that case ".com" , ".com/index.shtml" and that darned addons-one. Mostly it bugs me, that Firefox stores those addons-addresses too, even if it is never entered to address bar.

g1smd

4:16 am on Feb 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is expected behaviour for any URL you have visited before with that browser.