Mobile app version of vmapp.org
Login or Join
Lengel546

: How do I do a 301 redirect of specific pages with a catchall for any pages not specified? We are changing domains and need to redirect old pages to new, and then create a catch-all to redirect

@Lengel546

Posted in: #301Redirect #Htaccess

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:


Redirect www.olddomain.com/page1 to www.newdomain.com/page1. Redirect page2 the same way.
Redirect all other pages on oldomain.com to the home page of the new domain.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Lengel546

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

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 www.newdomain.com/page1 Redirect 302 /page2 www.newdomain.com/page2
# Redirect everything else to the home page
RedirectMatch 302 ^ 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 / 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$ www.newdomain.com/page1 [R=302,L]


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme