0

I have an .htaccess file that is shared between a number of websites. Each of these sites are served from the same set of files but load a different configuration based on the domain name they run on.

However, I need to add a number of 301 redirects and I'm running into a small problem. I only want the redirects to execute for a specific domain, not for each of the domains the .htaccess is used by. E.g. I want to redirect mysite1.com/some/path to redirect to mysite2.com/some/other/path but the problem is that if /some/path also exists for mysite3.com it's also redirected.

What I basically need is something like:

<if host = mysite1.com>
  Redirect /some/path http://mysite2/some/other/path
</if>

Is this possible in .htaccess?

2

It is possible, by adding a condition to your RewriteRule eg.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.othersite1.com$   [OR,NC]
RewriteCond %{HTTP_HOST} ^www.othersite2.com$   [NC]
RewriteRule ^some/path        http://mysite2/some/other/path [R]

What this does is:

  • Then it tests if the URL requested matches "some/path". Remember that in a .htaccess context the URLs are relative to the directory your .htaccess file is in. If this matches the conditions above it are avaluated.
  • Since the two conditions are OR-ed together, only one needs to match. In this example I have two domains I want this rule to redirect for. You can easily add more.
  • Finally the URL is rewritten, and redirected...
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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