elboriyorker
07-13-2024, 02:27 AM
A 301 redirect using an .htaccess file is a redirect that you create when you rewrite a piece of code on your site.
There are many reasons why you might want to input a 301 redirect on your site, but how do you do it, and why should you do it the .htaccess way? We’ve got the answers.
But before we go into specifics, let’s dive into 301 redirects.
What Is a 301 Redirect?
A 301 redirect is a permanent redirect. When a user tries to access an old URL, the server sends their browser the 301-Permanently Moved status code and sends them to another page. This is useful for site owners and users because it means they are directed to the next most relevant page.
301 redirects are useful for search engines too because they help to keep their indexes updated. They inform search engines that content has been moved and ensure that the visibility associated with the original page is transferred to the new URL.
For more information, take a look at our comprehensive SEO guide to 301 redirects (https://www.semrush.com/blog/301-redirects/).
What Is an .htaccess File?
An .htaccess file is a part of what controls the high-level configuration of your website. You edit the contents of your .htaccess file to enable and disable certain features of your server software without editing in the server configuration file directly. It’s a pretty easy way to make important changes, but you have to be really careful that you’re editing the code correctly. One mistake in your code can cause a lot of problems for users.
This means that a 301 redirect using an .htaccess file is a permanent redirect implemented via your .htaccess file rather than the server configuration file.
When Should You Use a 301 Redirect with an .htaccess File?
A 301 redirect using an .htaccess file can generally be used in three different scenarios:
To redirect visitors after you’ve moved to a new domain
To redirect visitors after you’ve moved old site pages to a new site structure
To redirect visitors to new pages after you’ve combined two sites
It’s pretty easy to do. We’ll walk you through how it works.
How to Do a 301 Redirect with an .htaccess File
A 301 redirect is executed differently depending on the server you use. The .htaccess method is usually used on Apache servers but can be implemented on other servers too.
How to Do 301 Redirects with .htaccess Files on Apache
Before you do anything, you need to locate the RewriteEngine, which you can find in the mod_rewrite module in Apache. You can load it using the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
Then you can add all your rules below “RewriteEngine Onâ€ÂÂ. The rules you add depend on what exactly you want to redirect.
Redirecting a Single URL
When redirecting a single URL, all you have to do is add this code (make sure you substitute “oldpageâ € and “newpageâ € for your page names!):
RedirectMatch 301 /oldpage/ /newpage/
Redirecting a Single Folder
When redirecting a single folder to a new location, you would use the following:
RewriteRule ^/?folder/(.\*)$ /location/$1 [R,L]
We’ve used “folder⠢‚¬Â and “locationà€ as our examples. Substitute your folder and location names!
Redirecting www to Non-www with a 301 .htaccess Redirect
Maybe you want to run a redirect because you don’t want to use a www subdomain. If this is the case, you’ll need to redirect to a non-www version. Here’s what you need; just replace “exampleâ € with your own domain:
RewriteCond %{HTTP_HOST} ^(.\*)$ http://example.com/$1 (http://example.com/%241) [L,R=301]
How to Do 301 Redirect with .htaccess Files on WordPress
Firstly, you’ll have to connect your site’s server to edit your .htaccess file. To do this, you have two options.
Connect to your server through FTP
Use the built-in File Manager tool, which you can find in cPanel
To use the built-in File Manager tool, log in to your cPanel dashboard and locate the tool. Then back up your .htaccess content and use the Edit function to add your 301 redirect code. This will take you to the text editor. From here, you can add the code you need.
Of course, the 301 redirect code you need depends on what you want to achieve.
Redirecting a Single Page
To redirect a single page to another one, you’ll need to add the following code, replacing “oldpageâ €ÂÂ, “exampleâ €ÂÂ, and “newpageâ € with your details:
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Redirecting a Domain Name
To redirect an entire domain name to another domain name, use the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)oldsite\.com$ [NC]
RewriteRule ^http://newsite.com%{REQUEST_URI} [L,R=301]
Just, as always, make sure that you put in your information instead of “oldsiteâ € and “newsiteâ €ÂÂ.
How to Do 301 Redirects with .htaccess Files in Windows
If you use Windows to run your site, we’ve also got you covered.
Redirecting a Single Page
To redirect a single page, type in the page name under the Pattern field. Your code might look something like this, with your details subbed in:
blog/page-name/
Redirecting an Entire Website
Redirecting your entire site to another domain is also super easy. You just need to add the following to your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newsite.com/$1 (http://www.newsite.com/%241) [R=301,L]
And replace “newsiteâ € with your domain name!
For more information, check out our article on the 16 crucial SEO steps when redirecting an entire website (https://www.semrush.com/blog/16-crucial-seo-steps-to-take-when-migrating-your-website/).
Redirecting Old URLs to New URLs
If you change your filenames for specific pages, you can redirect to the new URLs by inputting the following code and replacing “oldpageâ €ÂÂ, “exampleâ €ÂÂ, and “newpageâ € with your info:
redirect 301 /oldpage.html http://www.example.com/abc/newpage.html
How to Identify & Fix Redirect Issues
SEO issues aren’t foreign to any of us, and, like any other feature, redirects can cause problems too.
With our Site Audit tool (https://www.semrush.com/multilogin/?redirect_to=%2Fsiteaudit%2F), you can quickly recognize and resolve any problems you might have with your redirects.
The Site Audit tool can recognize problems like:
No redirect or canonical to HTTPS homepage from HTTP version
Redirect chains and loops
Pages with temporary redirects
Pages with a broken canonical link
Broken external links
Broken internal links
So, if you’ve just added some redirects to your site, here’s how to set up your Site Audit to find any technical issues and get them resolved.​
There are many reasons why you might want to input a 301 redirect on your site, but how do you do it, and why should you do it the .htaccess way? We’ve got the answers.
But before we go into specifics, let’s dive into 301 redirects.
What Is a 301 Redirect?
A 301 redirect is a permanent redirect. When a user tries to access an old URL, the server sends their browser the 301-Permanently Moved status code and sends them to another page. This is useful for site owners and users because it means they are directed to the next most relevant page.
301 redirects are useful for search engines too because they help to keep their indexes updated. They inform search engines that content has been moved and ensure that the visibility associated with the original page is transferred to the new URL.
For more information, take a look at our comprehensive SEO guide to 301 redirects (https://www.semrush.com/blog/301-redirects/).
What Is an .htaccess File?
An .htaccess file is a part of what controls the high-level configuration of your website. You edit the contents of your .htaccess file to enable and disable certain features of your server software without editing in the server configuration file directly. It’s a pretty easy way to make important changes, but you have to be really careful that you’re editing the code correctly. One mistake in your code can cause a lot of problems for users.
This means that a 301 redirect using an .htaccess file is a permanent redirect implemented via your .htaccess file rather than the server configuration file.
When Should You Use a 301 Redirect with an .htaccess File?
A 301 redirect using an .htaccess file can generally be used in three different scenarios:
To redirect visitors after you’ve moved to a new domain
To redirect visitors after you’ve moved old site pages to a new site structure
To redirect visitors to new pages after you’ve combined two sites
It’s pretty easy to do. We’ll walk you through how it works.
How to Do a 301 Redirect with an .htaccess File
A 301 redirect is executed differently depending on the server you use. The .htaccess method is usually used on Apache servers but can be implemented on other servers too.
How to Do 301 Redirects with .htaccess Files on Apache
Before you do anything, you need to locate the RewriteEngine, which you can find in the mod_rewrite module in Apache. You can load it using the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
Then you can add all your rules below “RewriteEngine Onâ€ÂÂ. The rules you add depend on what exactly you want to redirect.
Redirecting a Single URL
When redirecting a single URL, all you have to do is add this code (make sure you substitute “oldpageâ € and “newpageâ € for your page names!):
RedirectMatch 301 /oldpage/ /newpage/
Redirecting a Single Folder
When redirecting a single folder to a new location, you would use the following:
RewriteRule ^/?folder/(.\*)$ /location/$1 [R,L]
We’ve used “folder⠢‚¬Â and “locationà€ as our examples. Substitute your folder and location names!
Redirecting www to Non-www with a 301 .htaccess Redirect
Maybe you want to run a redirect because you don’t want to use a www subdomain. If this is the case, you’ll need to redirect to a non-www version. Here’s what you need; just replace “exampleâ € with your own domain:
RewriteCond %{HTTP_HOST} ^(.\*)$ http://example.com/$1 (http://example.com/%241) [L,R=301]
How to Do 301 Redirect with .htaccess Files on WordPress
Firstly, you’ll have to connect your site’s server to edit your .htaccess file. To do this, you have two options.
Connect to your server through FTP
Use the built-in File Manager tool, which you can find in cPanel
To use the built-in File Manager tool, log in to your cPanel dashboard and locate the tool. Then back up your .htaccess content and use the Edit function to add your 301 redirect code. This will take you to the text editor. From here, you can add the code you need.
Of course, the 301 redirect code you need depends on what you want to achieve.
Redirecting a Single Page
To redirect a single page to another one, you’ll need to add the following code, replacing “oldpageâ €ÂÂ, “exampleâ €ÂÂ, and “newpageâ € with your details:
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Redirecting a Domain Name
To redirect an entire domain name to another domain name, use the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)oldsite\.com$ [NC]
RewriteRule ^http://newsite.com%{REQUEST_URI} [L,R=301]
Just, as always, make sure that you put in your information instead of “oldsiteâ € and “newsiteâ €ÂÂ.
How to Do 301 Redirects with .htaccess Files in Windows
If you use Windows to run your site, we’ve also got you covered.
Redirecting a Single Page
To redirect a single page, type in the page name under the Pattern field. Your code might look something like this, with your details subbed in:
blog/page-name/
Redirecting an Entire Website
Redirecting your entire site to another domain is also super easy. You just need to add the following to your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newsite.com/$1 (http://www.newsite.com/%241) [R=301,L]
And replace “newsiteâ € with your domain name!
For more information, check out our article on the 16 crucial SEO steps when redirecting an entire website (https://www.semrush.com/blog/16-crucial-seo-steps-to-take-when-migrating-your-website/).
Redirecting Old URLs to New URLs
If you change your filenames for specific pages, you can redirect to the new URLs by inputting the following code and replacing “oldpageâ €ÂÂ, “exampleâ €ÂÂ, and “newpageâ € with your info:
redirect 301 /oldpage.html http://www.example.com/abc/newpage.html
How to Identify & Fix Redirect Issues
SEO issues aren’t foreign to any of us, and, like any other feature, redirects can cause problems too.
With our Site Audit tool (https://www.semrush.com/multilogin/?redirect_to=%2Fsiteaudit%2F), you can quickly recognize and resolve any problems you might have with your redirects.
The Site Audit tool can recognize problems like:
No redirect or canonical to HTTPS homepage from HTTP version
Redirect chains and loops
Pages with temporary redirects
Pages with a broken canonical link
Broken external links
Broken internal links
So, if you’ve just added some redirects to your site, here’s how to set up your Site Audit to find any technical issues and get them resolved.​