Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to do something like VirtualHosts using mod_rewrite on a GoDaddy hosted site. I have a single hosting account and two domains, a.com and b.com. I want requests for b.com/document.html to be served from ~/html/b_site/document.html (where ~/html/ is the DocumentRoot, serving as root for a.com). I found a reference to this use case in the mod_rewrite documentation suggesting that it is possible, but I've been unsuccessful. Here's the contents of ~/html/.htaccess:

RewriteEngine on
Options FollowSymLinks

RewriteCond %{HTTP_HOST} b\.com$
RewriteBase /
RewriteRule ^(.*)$ b_site$1

The result, when I point my browser at b.com: the location bar changes to b.com/b_site/ and the server responds with 500. The following RewriteRule works without any issues:

RewriteRule ^(.*)$ b_site/index.html

...which remains the only way I've been able to get anything but a 500 or warnings about redirect loops. The RewriteBase line seems to make no difference.

To find out what was being matched, I wrote a simple PHP script which would accept a query parameter and print it, and I used this RewriteRule:

RewriteRule ^(.*)$ b_site/index.php?q=$1

The output shows that in this case the RewriteRule matched "b_site/index.php". The RewriteBase documentation does imply that something like that should be happening, but again, the RewriteBase seems to make no difference in the result. Answers posed to similar questions all seem to boil down to the same mechanism that I'm trying.

Any thoughts?

share|improve this question

2 Answers 2

Your .htaccess context, as well as the RewriteBase, are both setting the context for the RewriteRule to /; this means your substitution would take a request for http://b.com/dir_a/page.html and serve it from http://b.com/b_sitedir_a/page.html - probably not gonna work.

From the documentation:

When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set.

Try this:

RewriteCond %{HTTP_HOST} ^b\.com$ [NC]
RewriteRule ^(.*)$ b_site/$1
share|improve this answer
    
Shane, thanks for responding. Adding the slash as you suggest, I still get a 500, but the browser location at least is not rewritten from b.com to b.com/b_site. I guess that indicates mod_rewrite was not finding the b_site$1 directory and was treating it as a URI. –  philo Oct 31 '11 at 15:05
up vote 0 down vote accepted

Solved. And no thanks to GoDaddy for making you pay for access to error logs!

This all started to make sense when I saw someone else's logs. It's now clear to me how the recursion is happening and why things are getting rewritten a second time (and infinitely recursing, causing the 500). There's a great solution to the recursion problem here (via serverfault user Dave Drager's answer to a similar question).

I simplified a little and arrived at this solution:

RewriteEngine on
Options FollowSymLinks
RewriteCond %{ENV:REDIRECT_X} =""
RewriteCond %{HTTP_HOST} b.com$ [NC]
RewriteRule ^(.*)$ b_site/$1 [E=X:1,L]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.