301 Reindirizzamento con caratteri jolly – Come applicarlo con .htaccess?

301 Reindirizzamento con caratteri jolly

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!

Per esempio:

  • “old-domain.com/page1will be redirected to “new-domain.com/page1
  • “old-domain.com/category/productwill be redirected to “new-domain.com/category/product
  • “old-domain.com/contact-uswill 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.

Per esempio:

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

It’s a good idea to use an “acquisition pagewhen 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.

Ovviamente, 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 domainhas 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. però, 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:

UN 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. però, 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. Infatti, they can help maintain rankings by redirecting outdated URLs to updated ones.

Can I use regular expressions in a wildcard redirect?

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

Divulgazione: Alcuni dei collegamenti nella pagina possono contenere un codice di riferimento (link di affiliazione), che dice all'altra parte che ti abbiamo mandato. Se ti è piaciuto quello che ti suggeriamo e fai un acquisto, potremmo ricevere una commissione.

Bozh
Sono Bozh, e ho creato siti Web con WordPress per 15+ anni. Da 2016, Ho lavorato con le migliori società affiliate, gestire la mia attività di e-commerce, e gestito diversi blog di nicchia. Ho fondato WPCtrl.com con l'idea di condividere le mie conoscenze, esperienze personali, e consigliarti alternative migliori.
WPCtrl.com