Ondigitals

Leading Agency
in South East Asia

Japan

China

Thailand

Taiwan

Vietnam

Philippines

Malaysia

Singapore

Indonesia

Australia

Insights

Guide to do 301 redirects with .htaccess files

SEO

28/04/2023

14

Definitions of 301 redirect & .htaccess file

Before we get into the details of how to do a 301 redirect with an .htaccess file, let’s take a look at what a 301 redirect and .htaccess files are.

Definition of 301 redirect with .htaccess files

Definition of 301 redirect with .htaccess files

301 redirect

A 301 redirect is a permanent redirect from one URL to another. When someone clicks on a link to a URL that has been changed, the browser will automatically take them to the new URL. This is important for SEO because search engines will recognize the new URL as the correct one and transfer any ranking or authority from the old URL to the new one.

.htaccess file

An .htaccess file is a configuration file used on Apache web servers to control various operation aspects of the server. It’s typically used to set up redirects, block access to certain files or directories, and set up custom error pages.

When should you use a 301 redirect with an .htaccess file?

There are several situations where you might want to use a 301 redirect with an .htaccess file. These include:

  • You have changed the URL of a page on your site, and you want to redirect visitors from the old URL to the new one.
  • You have moved your site to a new domain, and you want to redirect visitors from the old domain to the new one.
  • You have deleted a page on your site, and you want to redirect visitors to a different page on your site.

By using a 301 redirect in these situations, you’ll ensure that your visitors can still find the content they’re looking for, and also preserve any SEO authority that you’ve built up over time.

How to do a 301 redirect with an .htaccess file?

Now that you know when to use a 301 redirect with an .htaccess file, let’s take a look at how to do it.

How to do 301 redirect with .htaccess files

How to do 301 redirect with .htaccess files

On Apache

If you’re running your site on an Apache web server, you can use an .htaccess file to set up your 301 redirects. Here are steps that you have to follow:

  1. Log in to your server using an FTP client or cPanel file manager
  2. Locate the .htaccess file in the root directory of your website
  3. Add the following code to the top of the file:

<IfModule mod_rewrite.c>

RewriteEngine On

</IfModule>

  1. Add new rules after RewriteEngine On

Redirecting a single URL

To redirect a single URL, add the following code to your .htaccess file:

Redirect 301 /old-page.html https://www.yoursite.com/new-page.html

Replace /old-page.html with the old URL that you want to redirect, and replace https://www.yoursite.com/new-page.html with the new URL that you want to redirect to.

Redirecting a single folder

To redirect a single folder and all of its contents, add the following code to your .htaccess file:

RedirectMatch 301 ^/old-folder/(.*)$ https://www.yoursite.com/new-folder/$1

Replace /old-folder/ with the name of the old folder that you want to redirect, and replace https://www.yoursite.com/new-folder/ with the URL of the new folder that you want to redirect to.

Redirecting www to non-www with a 301 .htaccess redirect

Redirecting www to non-www with a 301 .htaccess redirect is a common practice in website management. It helps ensure that all traffic to your website is directed to a single, consistent domain.

To redirect www to non-www, you will need to modify your .htaccess file. Here’s how to do it:

  1. Open your .htaccess file for editing. You can do this using any text editor, such as Notepad or TextEdit.
  2. Add the following lines of code to the top of your .htaccess file:

RewriteCond %{HTTP_HOST} ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Replace “yourdomain.com” with your actual domain name.

  1. Save the changes to your .htaccess file and upload it to your website’s root directory using FTP or your website’s file manager.

Once the redirect is in place, all traffic to the www version of your domain will be redirected to the non-www version. This can help improve your website’s search engine optimization and ensure that all of your website’s visitors are accessing the same content.

It’s important to note that it may take some time for search engines to recognize and update their indexes with the new redirect. So, be patient and monitor your website’s traffic and search engine rankings to ensure that everything is functioning as expected.

How to rewrite 301 redirect with .htaccess files

How to rewrite 301 redirect with .htaccess files

On WordPress

To change your .htaccess file, you must first connect to the server hosting your website. You have two ways to do this:

  1. Use FTP to connect to your server.
  2. Use the integrated File Management program in cPanel.

Log in to your cPanel dashboard and look for the built-in File Manager tool there. Once you’ve made a backup of your .htaccess file’s content, use the Edit feature to include the 301 redirect code. You’ll access the text editor after doing this, where you can add the necessary code.

The 301 redirect code that you require will certainly depend on your goals.

Redirecting a Single Page

To redirect a single page, you need to add the following code to your .htaccess file:

Redirect 301 /old-page/ https://www.yourwebsite.com/newpage/

Replace /old-page/ with the old page URL and https://www.yourwebsite.com/new-page/ with the new page URL.

Redirecting a Domain Name

To redirect a domain name, you need to add the following code to your .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)oldsite\.com$ [NC]

RewriteRule ^http://newsite.com%{REQUEST_URI} [L,R=301]

Replace oldsite with the old domain and newsite.com with the new one.

You can do 301 redirect with .htaccess on Windows

You can do 301 redirect with .htaccess on Windows

In Windows

If you are using Windows, you can also create a 301 redirect using an .htaccess file. Here are some methods:

Redirecting a Single Page

To redirect a single page, you need to add the following code to your .htaccess file:

Redirect 301 /old-page/https://www.yourwebsite.com/new-page/

Replace “/old-page/” with the old page URL and “https://www.yourwebsite.com/new-page/” with the new page URL.

Redirecting an Entire Website

To redirect an entire website, you need to add the following code to your .htaccess file:

Options +FollowSymLinks

RewriteEngine on

RewriteRule (.*) http://www.newsite.com/$1 [R=301,L]

Replace http://www.newsite.com with the new domain name you want to redirect to.

Identify and Fix Redirect Issues

Redirect issues can occur for a variety of reasons, including changes to URLs, website redesigns, and changes to domain names. These issues can negatively impact your website’s SEO and user experience, so it’s important to identify and fix them as soon as possible. Here are some steps you can take to identify and fix redirect issues:

  1. Conduct a website crawl: Use a website crawler tool to identify any redirect errors or broken links on your website. Some popular website crawler tools include Screaming Frog, SEMrush, and Ahrefs.
  2. Check your Google Search Console: Google Search Console can help you identify any errors or issues with redirects on your website. Navigate to the “Coverage” and “Enhancements” sections to check for any issues.
  3. Use 301 redirects: If you’ve changed the URL of a page on your website, use a 301 redirect to redirect the old URL to the new one. This will ensure that any links pointing to the old URL will redirect to the new URL.
  4. Fix broken links: If you have any broken links on your website, fix them as soon as possible. Broken links can negatively impact user experience and reduce the credibility of your website.
  5. Test your redirects: Use a redirect checker tool to ensure that your redirects are working correctly. Some popular redirect checker tools include Redirect Checker and HTTP Status Code Checker.
  6. Monitor your website regularly: It’s important to regularly monitor your website for any redirect issues or broken links. This will ensure that you can quickly identify and fix any issues that arise.

By following these steps, you can identify and fix redirect issues on your website, which will improve your website’s SEO and user experience.

Check your websites regularly to identify and fix issues

Check your websites regularly to identify and fix issues

Final Thoughts

301 redirects can be a powerful tool for maintaining the SEO value of your website when making changes to your URL structure or domain name. By following the steps outlined above, you can easily create 301 redirects with .htaccess files on Apache, WordPress, and Windows. Remember to always test your redirects to ensure they are working properly and to monitor your website’s performance after making any changes.

On Digitals provide a variety of services that can help you build and maintain websites for your business. Contact On Digitals for reliable and complete web-related services.


Back to list

Read more

    NEED HELP
    with digital growth?
    Tell us about your business challenge and let's discuss together