3

We are changing domains and need to redirect old pages to new, and then create a catch-all to redirect any that we missed.

This needs to be done as a 301 redirect using .htaccess.

For example:

  1. Redirect http://www.olddomain.com/page1 to https://www.newdomain.com/page1.
  2. Redirect page2 the same way.
  3. Redirect all other pages on oldomain.com to the home page of the new domain.

1 Answer 1

2

You basically just need to perform the redirects in the order you've stated: 1, 2 and 3.

Assuming newdomain.com and olddomain.com point to different servers then you can use mod_alias:

Redirect 302 /page1 https://www.newdomain.com/page1
Redirect 302 /page2 https://www.newdomain.com/page2

# Redirect everything else to the home page
RedirectMatch 302 ^ https://www.newdomain.com/

However, redirecting to the home page is rarely a good user experience (and Google likely treats it as a soft-404 anyway). You could simply redirect /<other-url> to newdomain.com/<other-url> and allow the new site to handle it as a meaningful 404 - generally a better experience for users.

Note that Redirect is prefix-matching. Everything after the matched URL (eg. /page1) is appended onto the end of the target URL. If the old and new URLs are the same then you only need a single Redirect directive to redirect everything:

Redirect 302 / https://www.newdomain.com/

Note that these are obviously 302 (temporary) redirects. Change to a 301 (permanent) redirect only when you are sure it's working OK. 301s are cached hard by the browser, so you don't want incorrect redirects cached by the client.


If olddomain.com and newdomain.com are physically hosted on the same filesystem then you'll need to use mod_rewrite instead since you'll need to check the host header for the domain being accessed. For example:

RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^page1$ https://www.newdomain.com/page1 [R=302,L]

Obviously, it would be far more efficient if olddomain.com and newdomain.com point to different places.

3
  • Thank you. And yeah, I like your idea on not redirecting everything to the home page. If there are a lot of hits on those we can deal with them individually. What would be the best way of redirecting to the same URL on the new domain for anything not specified directly? Commented May 17, 2017 at 19:57
  • Include the same directive as above (ie. Redirect 301 / https://www.newdomain.com/) at the end of the other redirects. (Instead of the RedirectMatch directive.)
    – MrWhite
    Commented May 17, 2017 at 20:10
  • If this answered your question then please mark it as accepted (checkmark below the arrows on the left) to remove it from the unanswered question queue. You can also upvote answers you find useful. Thanks, much appreciated. :)
    – MrWhite
    Commented May 17, 2017 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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