Have a Question?
To ask a practical question:
To be able to help you with obstacles which occur while you are working for an employer or a client, we need a lot more input. That data is sensitive, therefor SEONAUTs have the option to ask the mentor 1on1 questions in private and to provide more details.
To unlock even more features:
To ask a theoretical question:
We love the SEOLAXY community an we provided free answers on YouTube for many years. Today it is physically not possible to answer all of them. But we are still commited to answer all theoretical questions and questions about the lesson in YouTube comments.
If you have a theoretical question about this lesson:
Lesson Transcript
Intro
Welcome back to the SEOLAXY YouTube channel! You are watching the free and comprehensive Ecommerce SEO course called SEOLAXY School. In this lesson, you are going to learn about the .htaccess file and why it is really important for SEO. Let’s start.
What is the .htaccess file?
So, what is the .htaccess file? It’s a special file that exists on online store’s servers, if the online store CMS is based on PHP and is using an Apache Server, like popular CMSs as WooCommerce, Magento, Shopware and many others. Think of it like a set of instructions that tells your online store how to behave. One of the most important things is, that it can manage 'redirects,' which is a fancy way of saying 'sending visitors from one URL to another.'"
Why are redirects important?
"Why are redirects important? Redirects help online stores rank better in Google search results and are crucial for maintaining ranking if you change the URL structure of an online store or if you are moving it to a new domain. Imagine you moved your store to a new location. You’d want to make sure your customers know where to go, right? Redirects do the same thing online. They make sure, anyone trying to visit an old URL gets sent to the new URL automatically."
301 vs. 302 Redirects
There are two main types of redirects you should know about: 301 and 302. The 301 redirect is a permanent redirect. A 301 redirect is like telling Google, 'Hey, this product page has moved for good.' It's like changing your store’s address when you move your store to a new address, you want all your potential customers, or in this case, your online store visitors, to go to the new place. This helps keeping most of your online store’s power, or what we call 'link juice.' in SEO."
The main .htaccess file of any website resides in the root folder. Files on servers starting with a dot indicate that those files are most of the times hidden in the file system. So if you don’t see it instantly in your root folder, you need to turn on visibility for hidden files. Let’s open one.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Before editing your htaccess file, it is always a good idea to make a backup of the current one, because this file is very sensitive and can cause many troubles if mistakes are made. All instructions should be written under the line which enables the Rewrite Engine.Here's how you can set up a 301 redirect in .htaccess, you first write the word “redirect”, then the type of the redirect and then the relative URL which needs to be redirected followed by the relative URL where it should be redirected to.
Redirect 301 /p/discontinued-product/ /p/succeeding-product/
Let’s say that you moved the category “basketball shoes” from the fashion category into the sports category. You want Google to know where they are now located, and also to redirect users who bookmarked this category in their browser not to get an error when visiting the bookmark.
Redirect 301 /c/fashion/shoes/basketball/ /c/sports/basketball/shoes/
This line of code means anyone going to the old basketball shoes category URL within the fashion category will automatically go to the new URL within the “sports” category instead".But sometimes, you might only want to temporarily send customers to a different product page.That’s where a 302 redirect comes in. It’s like saying, 'Hey, this is just for now, but we’ll be back!'. Maybe you have run out of a product in your online store and you know it will take a while to have it in stock again, so you can temporarily redirect the users to a very similar product and remove the redirect when the main product is available again.
A 302 redirect looks like this:
Redirect 302 /p/out-of-stock-product.html /p/alternate-product.html
You can use this for short-term changes, also when a subcategory is temporarily out of products and redirect it to the parent category.
Redirects and Link Juice
Now let's talk about something important called 'link juice.' Imagine your online store has a bunch of partners who send customers your way through backlinks. In the online world, when other websites create a backlink to your store, it gives your store power, or 'external link juice,' that helps it rank higher on Google if you do the internal link juice distribution well. We will talk about link juice in detail in one of the next lessons. This is why redirects are so important. Another important thing is to understand that if you use a 301 redirect, most of the link juice goes to the new URL. But here's the catch: you lose 30% of that power with every redirect. So, if your old product page had 100% link juice and you redirect it to a new page, the new page is going to get only 70% of the initial link juice. This means your new URL might not be as strong as the old one and rank lower in Google.
So use redirects only when you really need them. And if possible, try to avoid having multiple redirects in a row, because each time you do, you lose 30% of the remaining link juice. So if you have two redirects in a row, we call that “double redirects”. With double redirects, you are losing 50% of the link juice instead of 70%. This is especially bad if you have double redirects on a domain level.
Every online store has 4 main domain URLs:
- https with the www version
- https with the non-www version
- http with the www version
- http with the non-www version
Whether an online store is using the www version or the non-www version isn’t important. It just needs to be consistent throughout an online store. HTTP and HTTPS are protocol standards. The “S” in “HTTPS” indicates that the server is using an SSL certificate, which is essential for online stores, because only by using SSL in a checkout, user transactions can be secured. It is also a ranking factor, so be sure that your online store is using an SSL certificate. If an online store has no redirects setup, you will be able to open all four URL versions and you will not be redirected anywhere, which is the worst scenario. This is rarely the case, but still happens. More common is that an HTTP to HTTPS redirect exists, but the non-www to www redirect or the www to non-www redirect rule is missing. In most cases, both redirect rules exist, but are executed one after another which creates so-called “double redirects” on a domain level and the online store is losing 50% of its external link juice.
So, fixing this issue can give an online store a significant link juice boost and result in better rankings very fast. Since this is considered basic SEO knowledge, it is too often skipped and the potential is left out. This should be the first thing you check when doing an SEO audit of an online store after checking the robots.txt file. You can test that very fast by finding the main domain URL of an online store, which in most cases is "https://www.youronlinestore.com/" and check if the main domain URL is the domain URL version with the most external link juice. So you should analyze which https URL version has the biggest referring domain popularity, also called Ref DomPop. This is not the same metric as how many backlinks a domain has. For example: If a domain has 100 backlinks from one website, the metrics would show 100 backlinks and only 1 referring domain. It is more important from which domain backlinks come from than how many backlinks it has from that domain. If you get only one backlink from the New York Times, that backlink is more worth than 100 backlinks from a small blog.
Let’s take a look at the backlink analytics in our SEO tool Semrush for the guitar online store “Moore Guitars”. We see that the non-www version has more referring domains, so it should be set as the main domain URL. Let’s see if that is the case. Oh, nooo. It is the WWW version. So we were redirected from the non-www version to the www-version, that is also what the Chrome browser plugin “Redirect Path” is showing us. It is a 301 redirect. Maybe the other referring domains are stronger? Let’s check that too. We will sort the referring domains by the Semrush Authority Score and turn only active backlinks on. We had 87 and 80 referring domains, now Semrush is showing us only 83 and 69 active referring domains. We are not filtering nofollow backlinks out, because we know that it doesn’t matter if a link is dofollow or nofollow, what matters is if it is being clicked on. I have been teaching this for years and the Google Leak in 2024 confirmed it. If we take a look at the top 10 referring domains, we see similar authority scores, but in the first group, we see three guitar-related websites, and in the second group only one. So, choosing the non-www version would have been the right choice.
Now, let’s check if this online store has double redirects too. For that purpose, we will check the opposite of the main domain URL. In this case, it would be the http non-www version. Let’s do that. We will use the httpstatus.io tool for that purpose because the browser cache can ruin such tests and show false data in the Chrome extension if you are doing repeated tests. There it is, double redirects, to be exact, two 301 redirects. Users are being first redirected to the https version and then to the www-version. So, if this online store removes those double redirects, they would see a strong link juice boost on their online store and get better rankings.
It’s time to open the .htaccess file and remove the existing domain redirects if they exist and insert these instead, if your main URL should be the www-version:
# Redirect from any http non-www URL to the corresponding https www URL
RewriteCond %{HTTP_HOST} ^myonlinestore\.com [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.myonlinestore.com/$1 [L,R=301]
# Redirect from non-www https to www https
RewriteCond %{HTTP_HOST} ^myonlinestore\.com [NC]
RewriteRule ^(.*)$ https://www.myonlinestore.com/$1 [L,R=301]
And these if your main URL should be the non-www version:
# Redirect from any http URL (www or non-www) to the corresponding https non-www URL
RewriteCond %{HTTP_HOST} ^www\.myonlinestore\.com [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://myonlinestore.com/$1 [L,R=301]
# Redirect from www https to non-www https
RewriteCond %{HTTP_HOST} ^www\.myonlinestore\.com [NC]
RewriteRule ^(.*)$ https://myonlinestore.com/$1 [L,R=301]
Please replace the domain name with your domain and insert the code below the first “RewriteEngine On” instruction in your .htaccess file. Always use 301 redirects for domain redirects.
You can find these rules in the SEOLAXY Resources section or just pause the video and make a screenshot.
Again, remember to save a copy of your .htaccess file before making any changes. This way, if something goes wrong, you can easily go back to the original version."
307 and 308 Redirects
You may also see 307 and 308 redirects. They are usually used in Shop CMSes based on JavaScript frameworks, which we will cover in the SEOLAXY Academy lessons. Essentially, the 308 redirect is equivalent to a 301 redirect. They are both permanent redirects. As you might have already guessed, a 307 redirect is the same as a 302 redirect; both are temporary redirects.
Redirect Patterns for Moving to a New Domain
So far, we have covered single redirects and double redirects. Now we need to tackle redirect patterns, which are very useful when you move your online store to a brand new domain. This is a big change, and you want to make sure all your customers, and Google, know where to find your new store. This is the most common SEO mistake beginners make. They just forget to do the redirects right. The best way to do this is by setting up redirects that will send anyone from your old domain to your new one automatically.
Let’s say your old store was at `www.oldstore.com` and your new store is now at `www.newstore.com`. You’ll need to set up a rule in your .htaccess file that redirects all the URLs from the old store to the same URLs on the new store.
RewriteCond %{HTTP_HOST} ^(www\.)?oldstore\.com$ [NC]
RewriteRule ^(.*)$ https://www.newstore.com/$1 [L,R=301]
If it should be the non-www version, just delete the “www.” part in the second line. Again, make sure that these instructions are right below the first “RewriteEngine On” instruction inside the .htaccess file. So, if someone tries to visit `www.oldstore.com/shoes`, they’ll be automatically sent to `www.newstore.com/shoes`. This is really important because it helps to keep your customers happy and your new store’s SEO strong. It is very important that you don’t change the URL structure of your online store when moving to a new domain. For example, changing your product URLs from a category-based structure to a standalone structure.
Example:
From: https://www.yourstore.com/c/[category-slug]/[product-name]/
To: https://www.yourstore.com/p/[product-name]/
It is way easier and safer to keep your old URL structure when moving a domain. If you change it, be aware to create redirects with regular expressions (RegEx) in your .htaccess file on your old domain. AI tools like ChatGPT, Gemini, and Claude are good at assisting you to write those for individual cases. By setting up these patterns, you make sure that everyone, including Google, knows exactly where to find your products on your new domain. This helps you keep your search rankings and makes the transition to your new store smooth and easy for your customers.
Summary
If you have got value out of this lesson, please consider subscribing to our YouTube channel and joining SEOLAXY at seolaxy.com.
You have learned in this lesson what the .htaccess file is and how you can use it to create and modify redirects, which are very useful to save link juice in different ways, especially if you are changing the URL structure of an online store or are moving it to another domain. One more thing: It would be nice to hear how the .htaccess file saved or ruined some of your days. Let us know in the comments if this lesson was valuable to you and if you are missing something. If you have any questions, please don’t hesitate to ask them in the comments section.
In the next lesson, you are going to learn how to create SEO Optimized URL Structures through URL Rewriting. See you next time!