1

I'm trying to redirect some specific pages to the new ones on my new site (these redirects work fine) and, finally, my homepage OR (even better) my homepage and all the pages which I didn't redirect singularly to my new site home. This is how my .htaccess looks like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect 301 /url1 https://newurl1
Redirect 301 /url2 https://newurl2
Redirect 301 /url3 https://newurl3

A tried to add a single line like Redirect 301 / https://newsite.com but it doesn't work. I assume it's related to that RewriteRule which I found before?

Thank you for your help

MrWhite
  • 12,647
  • 4
  • 29
  • 41

1 Answers1

0

If you've migrated your entire site to a new server/domain then you presumably don't need the existing # BEGIN/END WordPress block - so this should be removed.

You can then use a series of mod_alias Redirect directives (as you were doing) and a RedirectMatch directive to redirect everything else to the homepage of the new site. Although probably just an example, but your existing Redirect directives are strictly invalid. The Redirect directive is prefix-matching and everything after the match is copied to the end of the target URL, so this can't be used to redirect many to one. Which is why we need to use the RedirectMatch directive for the last part, which matches against a regex instead.

For example:

# Specific redirects
Redirect 301 /url1 https://new.example/newurl1
Redirect 301 /url2 https://new.example/newurl2
Redirect 301 /url3 https://new.example/newurl3

# Redirect everything else to the homepage
RedirectMatch 301 ^/ https://new.example/

Note that, as mentioned above, the Redirect directive is prefix-matching, so the above would actually redirect /url1/something to https://new.example/newurl1/something. If that is a concern then you will need to use RedirectMatch throughout. Or use mod_rewrite.

Due to the prefix-matching nature of the Redirect directive, if you omit the trailing slash on the source URL, you should omit the trailing slash on the target. And likewise, if you include the trailing slash on the source then include the trailing slash on the target. Otherwise, you'll get a malformed redirect if redirecting multiple URLs.

It is advised to not mix directives from both mod_rewrite and mod_alias as you can get unexpected conflicts (since they aren't necessarily processed in the order the directives appear in the config file).

Reference:


Aside: It's not necessarily advisable to mass redirect to the home page (many to one). Search engines (ie. Google) will likely treat this as a soft-404 (and warn of this in GSC). It is confusing for users who don't see the page they are expecting. It is generally preferable to redirect to the same URL on the new site and provide a custom 404 (if the content has been removed) with information about the old/missing content with appropriate links to the home page and other pages on your site. This gives a better signal to search engines and a more meaningful response for users.

MrWhite
  • 12,647
  • 4
  • 29
  • 41