Michael Kwan

I write stuff

How to do a wildcard 301 redirect for a domain while keeping the same URL structure

Spread the love
  • 14
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    14
    Shares

Recently one of my clients did a rebranding and wanted to move all their content over to a new domain. Usually I would just do a regular 301 redirect for each page, but in this case they had hundreds of URLs and there was no way I was going to write hundreds of 301 redirects.

Thanks to the folks at Stack Overflow, I found a way to redirect all the URLs using a wildcard method so everything from www.olddomain.com/abcd redirected accordingly to it’s www.newdomain.com/abcd counterparts. I personally tested this with the Windows code for the web.config file for WordPress, but I’ve included the Linux .htaccess version as well for easy reference.

ALWAYS MAKE A BACKUP FILE BEFORE YOU START

 

Linux .htaccess version

Step 1: Access your file manager on cPanel, Plesk, or whatever control panel you’re using, and edit the .htaccess file (if you don’t know how to do this then you should contact your web developer instead)

Step 2: Add the below 301 redirect code and edit accordingly:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]

Step 3: Save and test.

Source: https://stackoverflow.com/questions/28894627/301-redirect-to-new-domain-with-same-url-structure

 

Windows web.config version

Step 1: Access your file manager on cPanel, Plesk, or whatever control panel you’re using, and edit the web.config file (if you don’t know how to do this then you should contact your web developer instead)

Step 2: Add the below 301 redirect code and edit accordingly:

<rule name="Redirect to new domain" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^olddomain(:\d+)?$" />
</conditions>
<action type="Redirect" url="https://my.newdomain.io/{R:1}" redirectType="Permanent" />
</rule>

Note that your web.config may already have a rewrite rule in it. Simply replace the old <rule name=”WordPress Rule” stopProcessing=”true”>Old code</rule> with the new one above.

Step 3: Save and test.

Source: https://stackoverflow.com/questions/31348272/redirect-old-domain-to-new-domain-keeping-url

 

Remember to notify Google via Google Search Console or Google Webmaster Tools so you do not lose SEO ranking due to this redirect! Here’s a quick tutorial if you don’t know how (jump to step 4): https://www.wpbeginner.com/wp-tutorials/how-to-properly-move-wordpress-to-a-new-domain-without-losing-seo/

Hope this helps! I’m not an expert programmer but post here if you encounter any problems and I’ll try my best to help. Or if anyone else can jump in, it would be very much appreciated 🙂


Spread the love
  • 14
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    14
    Shares
  •  
    14
    Shares
  • 14
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Michael

Leave a Reply

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

Back to top
Malcare WordPress Security