Wildcard redirect: What it is and why use it
There are moments when moving your site is inevitable. This could be due to a rebranding that involved a new domain name, a new CMS that uses a different URL structure or redirecting traffic to updated content.
But how do you make this process a breeze? How do you make sure the performance of your website on search engine results is not affected? How do you make sure your users never get the 404 errors?
This article explains how to move your site, or parts of your site, using a wildcard redirect.
More information about redirects in general
What is a wildcard redirect?
Also commonly known as a "catch-all subdomain" or "wildcard subdomain", it is a redirecting option that allows browsers to reach your website no matter what subdomain they requested (www.yourdomain.tld, yourdomain,.tld or even anyname.yourdomain.tld) without having to set up specific directories for those requests in advance.
Catch-all subdomains also allow browsers to redirect their request for a mistyped, non-existent, or random subdomain to a valid page on your site.
How to do a wildcard redirect?
Here are a few different ways that you can use wildcard redirects.
Using .htaccess for wildcard redirect
The .htaccess file is a configuration file for Apache servers. This file can contain many different settings like compression, caching, and can also be used for setting up wildcard redirects. Edit the file from your IDE, or using cPanel.
This method comes in handy when looking to do major redirects. For example, when planning to redirect a whole domain to another. It allows you to add redirects by directly modifying the .htaccess file manually.
Redirecting individual files
To redirect seo.com/news.htm to seo.com/media.htm. below is the 301 redirect to use:
Redirect 301 /news.htm /media.htm
Redirecting entire folders
Example: we've moved the /news section of our site to the /blog section. The structure within that folder remains the same. Here's how you redirect all traffic to the new location:
RewriteEngine on
RewriteRule ^old-folder/(.*)$ /new-folder/$1 [L,NC,R=301]
This will not only redirect /old-folder to /new-folder, but also /old-folder/article to /new-folder/article.
The R=301 command makes this a permanent redirect.Redirecting an old domain to a new domainRedirecting an old domain to a new domain
Redirecting an old domain to a new domain
Example: we want to redirect all traffic from our old domain old-seo.com to seo.com. It is good to note that doing this will move your entire website. Include the following code in the seo.com .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^seo.com [NC,OR]
RewriteCond %{HTTP_HOST} www.seo.com [NC]
RewriteRule ^(.*)$ http://seo.co/$1 [L, R=301,NC]
Redirecting a specific file to another domain
Example: we have moved a specific page to a new domain, and want to redirect all traffic to seo.com/news.htm to seo-new.com/media.htm. Below is the 301 redirect to use:
Redirect 301 /news.htm http://seo-new.com/media.htm
Forcing the use of a www. version
Search engines see seo.com and www.seo.com as two different sites. It is recommended to choose one and use a 301 redirect to the one you pick. Here is the code you will use to force the www version:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^seo.com [NC]
RewriteRule ^(.*)$ https://www.seo.com/$1 [L, R=301,NC]
Forcing the use of a non-www. version
If instead of using the www version of a site, you want to use the non-www version, use this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.seo.com [NC]
RewriteRule ^(.*)$ https://seo.com/$1 [L, R=301,NC]
Redirecting all files with a certain extension
Let’s assume you want to redirect all files with a .php extension, from seo.com/file.php to seo.com/file.htm. Use this code:
RewriteEngine on
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]
Redirecting files to SSL
In case you have added an SSL certificate to your website, it is a good idea to include 301 redirects that will send all HTTP traffic to HTTPS. By doing this, you make sure your users access non-duplicate and secure content.
Use this code to redirect files to SSL:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirects and status codes
As you've seen in the lines of code above, you can specify the redirect status code (301 for Moved Permanently, 302 for Moved Temporarily) in the redirect code. If the move is permanent, always use the 301 status code. This helps search engines understand that this is the new URL, so they'll use that one from now on. It also means that the SEO value of the old URL is carrier over to the new URL.
How to use cPanel to perform wildcard redirect
Using cPanel is a great idea when you want to redirect traffic from one domain to another. This can be done at both the webpage level (seo.com/about.html to seo.com/about-us/htm) and domain level (seo.com to seo.co).
Note that when you create a redirect in cPanel, a redirect rule is automatically included in the .htaccess file.
Redirecting a full domain to a new one
Here is what you need to do:
- Login to cPanel
- Go to the “Domains” section
- Tap the redirect button. This will open the Add Redirect page
- Tap the “Type” dropdown
- Select a permanent or temporary redirect
- Tap the next dropdown menu
- Choose the domain you want to redirect
- You can add essential folder names under the slash (/) field
- Go to the “redirects to” section and key in the address to which you want to redirect to
- Choose the desired redirect option (“only redirect with www”, “do not redirect www.”, and “redirect with or without www”
- To create a wildcard redirect, check the box
- The wildcard includes the file/folder name after the redirected URL, for instance, abc.com/tests.php redirects to ab.com/tests.php
- Tap add when you are done.
- The details of your redirect will pop up
How to use cPanel to change your .htaccess file
If you're using cPanel, here's how you can change the .htaccess file
- Log in to your cPanel
- Go to Files then File manager
- Choose website root folder
- In the folder you can right-click and select "Edit"
-
If you do not have .htaccess file, already, you need to create one. Here is how to create it:
- Tap “New File” on the top-left side
- Name the file. Make sure the file is named .htaccess (don’t forget to add the dot at the beginning and to write the filename in lower case)
- Set the directory of the new .htaccess file to /public_html/ or your website’s document root
- If your text editor displays an encoding dialog box, hit “edit.”
- Once you have access to .htaccess file (or after creating a new one), here are some 301 redirects that you can implement to realize different goals.
Making a smooth transition
With those code snippets, you can accomplish a lot of redirection needs but to make everything clean (and avoid running around in a lot of circles), consider following these guidelines when setting up redirects:
- Avoid “stacking” redirects when possible. Doing that is slower, and is not recommended for SEO. Instead, redirect to the correct URL directly.
- Before testing, make sure your hosting caches and local caches are cleared. Using a Chrome Incognito window is a good idea at this time because your browser will often cache redirects behind the scenes, hence preventing you from seeing changes to existing redirects instantly. If you are using a caching plugin, host-based cache, or Cloudflare cache, make sure that is clear too.
- If you are getting too many redirect loops with the Redirection plugin, contact your host about redirecting with .htaccess. The regular expression formatting is similar, but using .htaccess can ensure the annoying redirects are executed before the WordPress ones, which usually resolve any pending issues.
Wrapping up
Moving a website or domain can be tricky, and when not done correctly, significantly affect your SEO. It is a task that needs to be done cautiously and strategically, to avoid losing traffic and affecting your ranking on Google.
Using wildcard redirects makes this a breeze. They also come in handy when rebranding, not changing domains, or anything that may require a redirect. Most notably, wildcard SSL certificates will keep your website secure, especially after doing a wildcard subdomain redirect.