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

I have 2 domains that point to the same IP foo.com and bar.com

I don't have access to setting up a virtual host for the domains but I have managed to create .htaccess rules so that it is setup like this.

  • foo.com points to / directory
  • bar.com points to /bar directory

Having the setup above now I could have different index.html files for accessing the root of the domain.

This is the rule I implemented for bar.com

RewriteCond %{HTTP_HOST} ^(www\.)?bar\.com$
RewriteCond %{REQUEST_URI} !bar/
RewriteRule ^(.*)$ bar/$1 [L]

The above rule works perfectly however when I navigate to bar.com/bar it doesn't give me a 404 but instead it still points to the root of my bar directory.

So, I created another RewriteCond to handle such issue.

RewriteCond %{HTTP_HOST} ^(www\.)?bar\.com$
RewriteCond %{THE_REQUEST} "(GET|POST) \/bar\/ HTTP/"

While the RewriteCond works perfectly. I am not sure what rewrite rule I would write so that when the user visits bar.com/bar it would point to the /bar/bar directory and thus would give me a 404

I tried the RewriteRule below but it doesn't seem to work

RewriteRule ^bar/(.*)$ bar/bar/$1  [L,QSA]

So I temporarily created a redirect to the main bar.com domain.

RewriteRule ^bar/(.*)$ http://bar.com/$1 [L,QSA,R=301]
share|improve this question

1 Answer 1

This is how I did it.

RewriteCond %{HTTP_HOST} ^(www\.)?bar\.com$
RewriteCond %{THE_REQUEST} "(GET|POST) \/bar\/(.*) HTTP/"
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*)$ /bar/$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.