301 redirect for some pages and all other pages

Solution 1:

Since siteA.com and siteB.com point to different vHosts/servers then you can remove all the existing directives in siteB.com/.htaccess and use the simpler mod_alias Redirect and RedirectMatch directives.

For example:

# siteB.com/.htaccess

# Specific redirects
Redirect 301 /foo https://siteA.com/another-foo
Redirect 301 /bar https://siteA.com/another-bar
Redirect 301 /baz https://siteA.com/another-baz

# Redirect everything else to homepage on siteA.com
RedirectMatch 301 ^ https://siteA.com/

Test first with 302 (temporary) redirects to avoid potential caching issues.

NB: Redirecting everything else to the homepage on the other site will likely be seen as a soft-404 by search engines and isn't particularly helpful for users. It is often preferable to serve a more descriptive 404 instead.

My final requirement is no one can access siteB after adding the redirection.

Since everything is redirected, siteB.com is effectively inaccessible.

Reference:

  • https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
  • https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch