Htaccess: Redirect any page to a single URL

You may find yourself in the situation where you have to move domains. If this is the case for you, you want to redirect your visitors from the old domain to the new domain.

In most cases, the best way to do this, is to redirect from the old homepage URL to the new homepage via the .htaccess file:

Redirect 301 / https://www.example.com

In the above code, the homepage (for example: https://old.com) is redirected to https://example.com. However, if one is to visit https://old.com/contact, there will be no redirect. You can solve this by dynamically redirecting people to the right new URL on the new domain. For this to work, the URL structure of the new domain should match the old domain (generally speaking, for Worpress sites this should be the case).

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) https://example.com%{REQUEST_URI} [R=302,NC]

The above example will redirect https://old.com/example to https://example.com/contact.

But in my case, the new structure didn’t match the old structure. Hence, I wanted to redirect any page on the old domain to the homepage of the new domain. The following .htaccess code example did the trick:

RedirectMatch permanent .* https://example.com

This will redirect any page or subdirectory, regardless of its extension, to your new domain.

 

Author Bio

Thank you for your interest in my blog! On this miniblog, I write mostly short (technical) blog posts that might interest other people. Read more about me or feel free to contact me.

 

Leave a Reply

Your email address will not be published. Required fields are marked *