301 redirects seem easy to apply when you first think about them, but when you get to the actual task, things become more complicated.
Recently, I wanted to redirect one of my old domains to my active website. So, I looked through almost every article on how to make a 301 redirect on the top page of Google, but I couldn’t find a simple, easy-to-apply, working solution.
What is a Wildcard Redirect?
Before we dive into the technical details of applying a wildcard redirect using .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 target 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.
How to Set Up a 301 Wildcard Redirect Using .htaccess?
Only use this code if you want to keep 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”
apacheCopy codeRewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ https://newsite.com/$1 [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’re looking for:
apacheCopy codeRewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/ [R=301,L]
This code redirects all URLs from “old-domain.com” (with or without “www”) to the homepage of “new-domain.com”.
With this code in the .htaccess file of the old website, all URL variations, including paths or query parameters, will be redirected to the new website’s homepage.
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 isn’t related to your brand or website in any way. This helps keep things clearer for both visitors and search engines.
Of course, don’t forget to replace these examples with your actual domain names and URLs.
Make sure to thoroughly test the redirect after implementation to ensure it works as expected.
What if my “old domain” has subdomains that I also want to redirect?
This is a common scenario since some websites use subdomains to logically organize content, and this content may have gained some backlinks over time that you’d also like to redirect to your new domain.
This is a bit more complicated because you need to ensure two things:
- You’ve added a CNAME record with your domain registrar (if you’re using their default nameservers) or within your hosting.
- You’ve added the subdomain in your hosting panel so that you can access it via the file manager.
Many guides suggest that you don’t need to add a second .htaccess
file if your subdomain uses the same hosting, and the root .htaccess
will do the job.
But in my case, the redirect only worked when I added the .htaccess
file to the subdomain folder.
The code is pretty much the same, as is the process. Simply create a new .htaccess
file (assuming you don’t already have one on the subdomain) and add the following rule:
apacheCopy codeRewriteEngine On
RewriteCond %{HTTP_HOST} !^subdomain\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ https://newsite.com/$1 [L,R=301]
Don’t Forget to Back Up Your .htaccess File
Before making any changes, it’s important to back up your current .htaccess
file. This ensures that you can easily revert to the previous state if something goes wrong during the process.
Simply download the original file to your computer in case you break something and need to restore the file.
Example with the 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 continue accessing the content on the new site.
You might also want to read this article:
A 301 redirect is a permanent redirect that tells search engines that a webpage has been permanently moved to a new URL.
A wildcard redirect, which uses a placeholder 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 automatically be 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 passed on to the new one.
This means that the new site can maintain the search engine rankings and traffic that the old site had achieved.
However, it’s important to ensure that the old website/domain has high-quality content. Tools like Ahrefs or SEMRush can be used to check which backlinks a domain has.
Does a wildcard redirect affect my website’s SEO ranking?
When implemented correctly, wildcard redirects should not negatively impact your website’s SEO ranking. 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 matching URLs.