How to do 301 redirect for pages on specific domain?

In htaccess is there a way to do 301 redirects for individual pages for just one domain? I have multiple domains that go to the same directory so I dont want to interfere with any pages for any other domains except this one.

Right now Im doing as example:

redirect 301 /events.php

But events.php is used by other domains so it affects all of them, I just want to do that redirect just for How can I do that?


Indeed! That’s a great place for it but you can’t “aim” it at any specific domain - it’s hierarchical in that it works for that directory and every sub-directory. Good point, though, as IF the domain in question is a subdomain of the main domain, it can alter the Error Document for its directory/domain with the result that only it will be affected (as Sillysoft asked).



I use ErrorDocument in quite a lot of my .htaccess files and it works perfectly …

Apache 2.x


ErrorDocument is a CORE feature of Apache so, to implement such, you’d need access to the VirtualHost section of that domain to make that work.

Of course, you COULD implement your own 404 scheme using mod_rewrite (it’s not a lot of effort but certainly more than using ErrorDocument) and implement different 404’s for each domain.



Great, thanks! Worked like a charm. Have another quick question relating to this, can I do the 404 redirects per site? Currently Im using this for all sites:

ErrorDocument 404 /

But I only want to use it for a specific site. Can that be done and how?

Yes, use a RewriteCond to say that the rule only applies when the domain is the one you’re targeting.

RewriteCond %{HTTP_HOST} ^www\\.domain\\.com
RewriteRule ^/?events.php [R=301,L]