Jul
19

How to Setup htaccess 301 Redirect: A Beginners' Guide

Thinking of moving your current URL to a new one? Here is a beginner’s guide on how to setup htaccess 301 redirect.

Are you planning a switch from an old URL to a new one? Well, there are a couple of things you need to do. Moving your business to a new website can bring a lot of challenges, and it is almost impossible to reach out to every customer about the new changes. The best way to minimize issues during your transition is to set up an htaccess 301 redirect. It is a permanent way of redirecting traffic from one URL to another. If all this sounds foreign to you, don’t worry. We will show you here how to setup htaccess 301 redirect.

How to Setup htaccess 301 Redirect the Right Way

Before we can look at how to setup htaccess 301 redirect, it is important to understand scenarios that might necessitate a 301 redirect. Some common uses of an htaccess 301 redirect include the following:

  • After transferring your old site to a new site structure, a 301 redirect will help you send users to the new web pages.
  • When you move to a new domain, an htaccess 301 redirect will divert your web users to the new home.
  • After merging two sites, a 301 redirect will allow you to send visitors to the new site.

1 Accessing the .htaccess File

Before you touch anything, it is important to note that executing an htaccess 301 redirect is only possible if your website is hosted on an Apache-based server.

As mentioned above, the best way to manage redirects is to use .htaccess files. You can create these files using your cPanel’s File Manager. But you should keep in mind that you need to make some configuration on the cPanel to make the .htaccess file visible. You will just allow all hidden files to be seen. If you prefer a simpler way, then you can use an htaccess generator, such as the one from FreeWebTools. The tool will automatically generate the necessary code for the .htaccess file to ensure the redirection works smoothly. 

To access the .htaccess file, follow these steps:

  1. Login to your cPanel.
  2. Search for the File Manager icon and open it.
  3. Once opened, go to Settings and search for the option to show hidden files. This process might differ from host to host, but it is usually under Preferences. You save the changes after selecting Show Hidden Files.
  4. Your File Manager will now show all files. So, you can go to the Files panel and open the public_html item.
  5. The public_html folder will now display its content, which includes your .htaccess file. Most likely, you won’t have an .htaccess file yet, meaning you need to create one for the new site.

With that done, it's also crucial to find the RewriteEngine, which will be useful in executing new rules. They are usually found within Apache. Just open the mod_rewrite module. Executing this code will help you start the RewriteEngine:

<IfModule mod_rewrite>

RewriteEngine On

</IfModule> 

After that, use the relevant rules, depending on your redirection needs. These rules will appear in a line below ‘RewriteEngine On’. Let’s now look at how a 301 redirect with .htaccess files works.

Redirect a Single URL

To activate a single URL redirect, you can add the following codes or rules a line after the RewriteEngine phrase:

Redirect 301 /outdated-page/https://www.mydomain.com/refreshed-page/

Be keen to change ‘mydomain.com/refreshed-page’ and use the correct URL address for your website. The old web address will be part of the first section, which in our case is ‘outdated-page’. The outdated-page should be preceded by a slash. Sometimes, you may want to include the file path of your old page if it is not found at the top level of the directory. The second section of the above code carries the full URL of the new site.

Setting Up a 301 Redirect for a Single Folder

To carry out the transfer of your folder to a new site, add this rule to the RewriteEngine script:

RewriteRule ^/?blog/(.\*)$ /news/$1 [R,L]

In the above rule, ‘blog’ and ‘news’ are just examples. So, you need to replace them with actual URLs. In the above scenario, the ‘blog’ subfolder is being redirected to the ‘news’ subfolder.

Redirecting the Entire Domain

Now that you know how to setup htaccess 301 redirect for pages and folders, you may also want to know how to transfer your whole site to a new domain. If that is the case, activate the following script on your .htaccess document:

Redirect 301 / http://www.new-domain.com/

You will replace ‘http://www.new-domain.com/with your new location address. To signal the server that there isn’t anything more to execute, include an extra line after the code. 

Using an htaccess 301 Redirect to Transfer WWW URLs to Non-WWW URLs

Sometimes, a www subdomain may not be ideal for you. In that case, you may institute a redirect to your desired web structure. In this case, you will redirect your existing www-based URL to a non-www version.

How to Setup htaccess 301 Redirect for WordPress

For basic users, you can carry out 301 redirects for your WordPress website by installing the Simple 301 Redirect plugin. This way, you can automate the process by just filling out simple forms.

With that said, you can perform an htaccess 301 redirect in WordPress. This is the most suitable option if you want to move the entire site to a new domain. To carry out this important task, just follow the steps below (Nearly all Apache-based hosts have similar steps, but we will be using Bluehost for this illustration):

  1. Log into your cPanel and then scroll down until you spot the File Manager icon. Just click open it to access directories.
  2. Next, find where the .htaccess file is in the available directories. It is found under the public_html directory.
  3. Now that you have found the required file, right-click on it to give you a list of options. You will choose Edit to open the htaccess editor. 
  4. It will open the .htaccess editor. So, look for the 'RewriteEngine On' line and add the relevant redirection rules. You will create the relevant rules based on your redirection needs, whether it is a folder, the entire site, or just a page.

To redirect a single page, look for the 'RewriteEngine On' line and then add this code:

Redirect 301 /outdated-page/ https://www.mydomain.com/refreshed-page/

Just like for a regular, non-WordPress site, you will substitute https://www.mydomain.com/refreshed-page/ and ‘outdated-page’ with your website’s actual URLs.

And if you want to redirect the whole domain of your WordPress site to a new one, just add the code as you would for a normal website. 

Again, substitute the dummy names used in the initial example of an entire domain redirect for the normal website with the actual URLs on your site.

Frequently Asked Questions (FAQs)

What is a 301 redirect?

When you want to redirect traffic from your old site to a new one, you will need to perform a 301 redirect. So, when visitors try accessing the old URL, their browser will redirect them to the new site. 

For this reason, a 301 redirect is useful to your users because it guarantees a seamless transition, sustaining a positive user experience. Besides this, search engines will also be able to notice the changes and transfer the already-built authority from your old site to the new URL.

Leading search engines are downgrading websites that point several domains to a single page without using a proper redirect.

When should I perform a 301 redirect?

You can perform a 301 redirect in several situations, which include the following:

  • When you want to migrate your entire site to a new domain
  • When you have a new URL and you want to divert traffic from the existing URL to the new one
  • When you want a more secure website, i.e., changing the existing HTTP framework to the more secure HTTPS by redirecting HTTP requests into the secure standard
  • When you have pages ready for deletion and want to redirect them to the home page or other relevant pages
  • When you want to do away with or merge similar pages that compete with each other

What is the purpose of an .htaccess file?

When you need to change critical aspects of your domain, you will need the .htaccess to help you execute the task. With the file, you can modify URL structure, personalize error pages, and, of course, redirect URLs, among others. 

Using it can simplify how you make changes to your site. However, you should be keen on how you edit rules, as one mistake can cause havoc on the whole website.

Final Remarks

Congratulations. Now you know how to setup htaccess 301 redirect for seamless transitions. Whether you are migrating the whole site to a new domain or changing the URL of a single page, an htaccess 301 redirect is usually preferred for web traffic redirection.

But don’t overdo it. If you are migrating the whole domain, ensure your new page is somewhat similar to the old one. The reason is that you don’t want to hurt your SEO ranking. 

Besides this, you also want to check your new site for broken links after completing a redirect. Finally, if you are experiencing a challenge while using the server, contact your web host for assistance.

Contact

Missing something?

Feel free to request missing tools or give some feedback using our contact form.

Contact Us