301 Wildcard Redirect – How to Apply It with .htaccess?

301 Wildcard redirect

301 redirects may look easy to apply when you first think about doing one, but when you face the real task, things get more complicated.

Recently I wanted to redirect one of my old domains to my active site, so I looked at almost all articles on how to do a 301 redirect on the top page of Google, but I couldn’t find an easy-to-apply, working solution.

What is a Wildcard Redirect?

Before we dive into the technicalities of applying a wildcard redirect with .htaccess, let’s first understand what it means.

A wildcard redirect is a type of redirect that allows you to forward multiple URLs or specific patterns to a single destination URL. Instead of creating individual redirects for each URL, you can use a wildcard character to capture and redirect entire groups of URLs with a single rule.

So if a user visits https://oldsite.com/sample-page, they will be automatically redirected to https://newsite.com/sample-page.

How to place a 301 Wildcard redirect via .htaccess?

Use this code only if you want to preserve the old URL path!

For example:

  • “old-domain.com/page1” will be redirected to “new-domain.com/page1”
  • “old-domain.com/category/product” will be redirected to “new-domain.com/category/product”
  • “old-domain.com/contact-us” will be redirected to “new-domain.com/contact-us”
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ https://newsite.com/ [L,R=301]

If you want to redirect any URL from the old domain to the homepage (or acquisition page) of your new domain, here is the code you are looking for:

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

This code will redirect all URLs from “old-domain.com” (with or without “www”) to the home page of the “new-domain.com”.

With this code in the .htaccess file of the old site, all URL variations, including any paths or query parameters, will be redirected to the homepage of the new site.

For example:

  • “old-domain.com/page1” will be redirected to “new-domain.com/”
  • “old-domain.com/category/product” will be redirected to “new-domain.com/”
  • “old-domain.com/contact-us?ref=123” will be redirected to “new-domain.com/”

It’s a good idea to use an “acquisition page” when redirecting an expired domain or a domain that is not connected in any way with your brand and website. This helps make it more clear for visitors and search engines.

Of course, don’t forget to change these examples with your real domain names and URLs.

Make sure to test the redirect thoroughly after implementing it to ensure that it’s working as expected.

What if my “old domain” has subdomains that I also want to redirect?

This is a common scenario since some sites use subdomains to divide the content more logically and over time this content may have gained some backlinks that you’d also want to redirect to your new domain.

This is a little bit more complicated because you have to make sure of 2 things:

  • You have added a CNAME record in your domain registrar (if you use their default nameservers) or within your hosting.
  • You have added the subdomain in your hosting panel, so you can access it from the file manager

Lots of guides over there suggest that if your subdomain is using the same hosting you don’t need to add a second .htaccess file, and only the root one will do the job.

But in my case, the redirect started to work only when I added the .htaccess to the subdomain folder.

The code is pretty much the same, also the process. Just create a new .htaccess file (assuming you don’t have one on a new subdomain) and add the rule:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^subdomain\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ https://newsite.com/$1 [L,R=301]

Don’t forget to backup your .htaccess file

Before making any changes, it’s essential to create a backup of your current .htaccess file. This ensures that you can easily revert to the previous state if something goes wrong during the process.

Just download the original file to your computer in case you broke anything and want to restore the file.

Example using cPanel file manager

301 Wildcard redirects for SEO

In SEO, maintaining consistent URLs is crucial. However, there are times when website owners need to change their domain name, move content to a new location, or update their website structure. In such cases, implementing 301 redirects is essential to ensure that both users and search engines can still access the content on the new website.

You might also want to check this article:

A 301 redirect is a permanent redirect that tells search engines that a web page has been permanently moved to a new URL.

A wildcard redirect, which uses a wildcard character (*), redirects all URLs from one domain to another.

This means that if someone tries to access a page on the old website, they will be automatically redirected to the corresponding page on the new website.

Using 301 wildcard redirects can be very beneficial for SEO, as it ensures that the link juice and authority of the old website are transferred to the new website.

This means that the new website can retain the search engine rankings and traffic that the old website had earned. However, it’s important to ensure that the old website/domain has high-quality content. You can use tools like Ahrefs or SEMRush to see what backlinks a domain has.

Will a wildcard redirect affect my website’s SEO rankings?

When implemented correctly, wildcard redirects should not negatively impact your website’s SEO rankings. In fact, they can help maintain rankings by redirecting outdated URLs to updated ones.

Can I use regular expressions in a wildcard redirect?

Yes, you can use regular expressions to create more complex wildcard redirect patterns, allowing for greater flexibility in URL matching.

Disclosure: Some of the links on the page may contain a ref code (affiliate links), that tells the other side that we have sent you. If you liked what we suggest and you make a purchase, we may receive a commission.

Daniel Adamo
Daniel is a content creator specializing in WordPress themes and plugins. He writes in-depth guides and tutorials to help both beginners and professionals get the most out of WordPress. In his spare time, Daniel acts as a volunteer for nonprofit organizations. He believes open source platforms like WordPress are important tools for empowering causes and communities around the world.