Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've done a little research into ModRewrite rules and conditions but have not been able to find a satisfactory set of rules/conds which achieve the effect I'm looking for.

Essentially, I have a directory on domain A ( which I would like to redirect to a different directory on domain B (

Note that I only want to apply the rewrite rule if the user is attempting to access /dir/ on domaina. If they are accessing a different directory or root I do not want to rewrite the URL.

Thank you!

share|improve this question

In domaina config:

#redirect everything under dir to the same location under
RewriteRule ^/dir/(.*)$$1 [r=301]

#redirect url to
RewriteRule ^/dir(/)?$ [r=301]

I didn't test this, but it should work.

If you're doing this in the root .htaccess file or in a specific directory config you'll want to drop the leading '/' or otherwise adjust the rule pattern. Something like ^dir(/)?$.

share|improve this answer
You could put both rules into one line with something like RewriteRule ^/dir(/?|/.*)$$1 [r=301] – matthew Jun 16 '10 at 18:24

Your Answer


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.