Forum Moderators: phranque

Message Too Old, No Replies

htaccess virtual hosts

problem with multiple domains

         

KingMacro

6:20 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



i have multiple domain names, and rather than configuring multiple virtual hosts in the apache config file i am trying to use htaccess to automatically rewrite the domains correctly.

i have for example:

www.domain1.com
www.domain2.com
www.domain3.com

i thenn have 3 folders inside the root directory for the web server of

/domain1/
/domain2/
/domain3/

currently i have a php script in the root of the web server which redirects to the correct directory, so the users get redirected to

[domain1.com...]
[domain2.com...]
[domain3.com...]

but this doesn't look very good, so i would like to use Rewrites to achieve this, this is what i have come up with so far:


RewriteEngine on
# append hostname to URL
RewriteRule (.*) %{SERVER_NAME}\%2f$1
# strip leading www. if it exists
RewriteRule ^www\.(.*) $1
# remove .com/.net/.org extensions
RewriteRule ^([^/]*).net(.*) $1$2
RewriteRule ^([^/]*).com(.*) $1$2
RewriteRule ^([^/]*).org(.*) $1$2

this should rewrite the url to /www.domain1.com/ then remove the www. and .com to give me /domain1/, allowing me to access [domain1.com...] and get the same page i would have gotten for [domain1.com...] however i am simply getting a 404 error for /domain1/ (which i know exists - because i can access it with the rules disabled)

anyone see what the error is in my rewrite rules?

jdMorgan

7:00 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



KingMacro,

Welcome to WebmasterWorld!

A simpler approach might work better:


RewriteEngine on
# Get domain from request HTTP_HOST header
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.(com¦net¦org)
# Rewrite the request to domain-specific subdirectory
RewriteRule (.*) /%2/$1 [L]

Jim

KingMacro

7:38 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



that appeared to do... well, nothing - it was as if there where no rewrite rules there, lol

i think the problem is with the fact that i used %2f in place of the / - however if i try to use a / then i get a 500 error so thats why i used %2f instead

i managed to get the server to do something by changing the com¦net¦org in the example you gave to *

RewriteEngine on
# Get domain from request HTTP_HOST header
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)(.+)
# Rewrite the request to domain-specific subdirectory
RewriteRule (.*) /%2/$1 [L]

however, that something was a 500 error until i changed the last line to

RewriteRule (.*) /%2\%2f$1 [L]

when it gave a 404 instead

jdMorgan

12:56 am on Feb 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot a couple of points:

First, the code may loop unless this is explicitly prevented:


RewriteEngine on
# Prevent "infinite" rewrite loop
RewriteCond $1 !^(domain1¦domain2¦domain3)/
# Get domain from request HTTP_HOST header
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.(com¦net¦org)
# Rewrite the request to domain-specific subdirectory
RewriteRule (.*) /%2/$1 [L]

I also forgot to note that you must replace all broken pipe "¦" characters with solid pipe characters before trying to use code copied from this board -- Posting on this board modifies those characters, and the broken pipe will cause 500-Server Errors.

If you still get a 500-Server Error with the modified code above, then please report the contents of your server error log file.

Jim

KingMacro

2:48 am on Feb 27, 2005 (gmt 0)

10+ Year Member



thank you very much - it worked perfectly.

I think the reason the first one didn't work was the infinate loops, however its hard to tell because my error logs get about 50 entries a second (i should really clean up my php scripts a bit... lol)

jdMorgan

4:25 am on Feb 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> 50 entries a second

Yes, it sounds like a cleanup is in order. Error logs should be clean if they are to be of any use in detecting new problems quickly.

Jim

nitric

2:20 am on Mar 14, 2005 (gmt 0)



I'm having a problem with this setup...

the .htaccess file works great, thanks for posting this script. but i'm having trouble with the cgi-bin.

/cgi-bin/

/htdocs/domain1/
/htdocs/domain2/

domain2 isn't resolved yet (just got it) but i can't access the cgi-bin from domain1/cgi-bin

does anyone know how to get it to redirect to the /cgi-bin/ or how to setup the .htaccess file to allow multiple cgi-bin diretories?

jdMorgan

2:06 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nitric,

The most likely cause is that your cgi-bin directory is aliased in httpd.conf. So, requests for /cgi-bin are "rewritten" before your .htaccess code can have any effect on them.

The simplest solution is to use a different name, like "cgi_bin" or "cgi-b". Be sure to lock down the file permissions of your new cgi-bin directories, and consider adding password protection to the execute-only scripts; The reason many hosts alias the cgi-bin directory is so that the actual files can be placed in a directory which is not directly Web-accessible.

Jim