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.
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 sitesIt’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 [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 cPanelTo 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.htmlRedirecting a Domain Name
To redirect an entire domain name to another domain name, use the following code:
RewriteEngine OnRewriteCond %{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 +FollowSymLinksRewriteEngine onRewriteRule (.*) http://www.newsite.com/$1 [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.
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.htmlHow 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, 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 linksSo, 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.
Open the Site Audit tool and click “New Site Audit” to create a new project. Give your new project a name: Configure your basic settings and then let the Site Audit run. Once your crawl has finished, head over to the Site Audit dashboard to see what you can discover.By clicking on “Top Issues”, you’ll be able to see all your issues at a glance. You can even filter by the check you’re interested in:
And to find out more about any of these issues, you just have to click on them.
Once our Site Audit tool has told you what to be on the lookout for, solving your issues is going to get a whole lot easier, and you can carry on inputting redirects with confidence.
Key Takeaways
Now that we’ve discussed the ins and outs of 301 redirects using an .htaccess file, let’s review:
Always double-check the edits you make to your .htaccess file before you go live. One typing mistake could cause a lot of problems for your users. Remember that a 301 redirect is executed differently depending on the server you use. You may need to use different pieces of code depending on how you run your site. If something goes wrong, you don’t need to panic. Once you’ve inputted your 301 redirects with .htaccess, turn to the Semrush Site Audit tool for help in spotting any issues and resolving them.Now it’s time to get started on your 301 redirects with .htaccess. They might be easier than you expect!
Innovative SEO services
SEO is a patience game; no secret there. We`ll work with you to develop a Search strategy focused on producing increased traffic rankings in as early as 3-months.
A proven Allinclusive. SEO services for measuring, executing, and optimizing for Search Engine success. We say what we do and do what we say.
Our company as Semrush Agency Partner has designed a search engine optimization service that is both ethical and result-driven. We use the latest tools, strategies, and trends to help you move up in the search engines for the right keywords to get noticed by the right audience.
Today, you can schedule a Discovery call with us about your company needs.
Source: