Forum Moderators: phranque

Message Too Old, No Replies

multiple domains & .htaccess

setting up multiple domains in .htaccess

         

lars0n

4:48 pm on Sep 13, 2006 (gmt 0)

10+ Year Member



Hi,

I signed up for webspace on Powweb and registered two domain names. I was told I could use both domains but that I would have to buy a tool that would manage the .htaccess file. I thought I could maybe save the 30 bucks and figure it out myself, but I'm struggling. Here's what I've got in the .htaccess file now:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.domain1.org$
RewriteRule ^(.*)$ [domain1.org...] [R=301,L]

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

And when I type in the address (for either one), I get:

"Redirection limit for this URL exceeded. Unable to load the requested page. This may be caused by cookies that are blocked."

?

jdMorgan

2:33 pm on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because your code will run again after the client follows the redirect, and therefore the server will return another 301 redirect response, and another, and another...

You'll need to explicitly exclude previously-redirected requests. I'd also suggest that you don't use a redirect at all, because this will 'expose' your folder structure to visitors, and make your URLs 'look funny.' Instead, simply rewrite the requests to the appropriate subfolder:


RewriteCond $1 !^folder1/
RewriteCond %{HTTP_HOST} ^www\.domain1\.org
RewriteRule (.*) /folder1/$1 [L]
#
RewriteCond $1 !^folder2/
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteRule (.*)/folder2/$1 [L]

If you are willing to use a standard and unique domain-subfolder naming convention that is tied to the domain names, then you can rewrite many domains with only one rule. For example, if you name your domain-subfolders "ds_<domain_name_here>", then this will rewrite all domain requests (except for your main domain) to subfolders if they exist:

# prevent rewrite loop
RewriteCond $1 !ds_
# check for main domain (to be excluded from rewrites)
# RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com
# extract domain name
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.([^.:]+)
# check that domain-subfolder exists
RewriteCond %{DOCUMENT_ROOT}ds_%1%2/ -d
# rewrite to domain subfolder
RewriteRule (.*) /ds_%1%2/$1 [L]

This will rewrite a request for any domain other than your main domain to a subfolder whose name is derived from the domain name, as long as that subfolder actually exists. So www.foo.com/bar.html or foo.com/bar.html is rewritten to /ds_foocom/bar.html. I omitted the period in the domain-subfolder name so as to avoid problems with detecting filetypes in subsequent rewrite code -- You can modify the code to include them if you so wish. The code also does not care if the requested domain begins with "www" or not -- "www" is omitted from the subfolder name, and the same subfolder is used for "www" or non-www domain requests. You can modify that, too, if you wish.

Another point is that you may indeed wish to move your main domain into a subfolder as well, to ease site maintenance. To do that, all you have to do is omit the second RewriteCond and move the physical files into an appropriately-named subfolder.

This code is untested, and may be missing a slash here or there. But it should serve as a good starting point.

Jim