You'll need to use mod_rewrite (RewriteRule
and RewriteCond
) directive(s) instead, as opposed to a mod_alias Redirect
directive, in order to check the requested Host
header.
For example, try the following near the top of the .htaccess
file, before the # BEGIN WordPress
section (the order of directives is important):
RewriteCond %{HTTP_HOST} ^foo\.org [NC]
RewriteRule ^events$ https://bar.org/events/ [R=301,L]
This redirects only http(s)://foo.org/events
to https://bar.org/events/
and ignores any other requested hosts, such as subsite1.foo.org
. Note that the trailing slash is omitted on the source URL (as in your later examples), but included on the target URL. Your first example omits the trailing slash on both the source and the target URLs?
UPDATE: The redirect should work whether or not there is a trailing slash
In that case, you should change the RewriteRule
pattern to read ^events/?$
in order to match both /events
and /events/
and redirect to bar.org/events/
in both cases. In other words:
RewriteRule ^events/?$ https://bar.org/events/ [R=301,L]
(Including the preceding RewriteCond
directive as before.)
You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirects will have been cached by the browser. Test first with 302 (temporary) redirects to avoid potential caching issues.
Note that the Redirect
directive is prefix-matching, so your previous Redirect 301 /events https://bar.org/events/
directive would also redirect /events/foo/bar/baz
to https://bar.org/events//foo/bar/baz
(note the erroneous double slash). To replicate the same behaviour with the above RewriteRule
, but less the double slash issue, you could change the RewriteRule
directive to read:
RewriteRule ^events(?:/(.*))?$ https://bar.org/events/$1 [R=301,L]
This would redirect /events
to bar.org/events/
, /events/
to bar.org/events/
and /events/<something>
to bar.org/events/<something>
.