How to Use htaccess Files for 301 & 302 Redirects Effectively

.htaccess Files and 301 & 302 Redirects

It is common practice to redirect one URL to another. When doing this, it is critical to observe best practices in order to maintain SEO value.

MOZ

.htaccess Files

The .htaccess file is an Apache web server configuration file that when detected by the web server will execute whatever rules are within the .htaccess file. This configuration file can be used to alter how the web server responds to user requests, by either enabling or disabling certain functions on the web server. An .htaccess file is a very powerful and simple tool in your arsenal to protect and administer your website too, so getting to know all the rules you can place within an .htaccess file will be very beneficial to your website administration.

So what can the .htaccess file actually do for me?

The .htaccess file has many uses, and each use depends entirely on the setup of your web server and what you’re trying to achieve. Below are a few things the .htaccess can do:

  • Redirects
  • Deny access by IP
  • Deny access by referrer
  • Hot link prevention
  • Blocking offline browsers and “bad bots”
  • Adding MIME types
  • Password protection
  • Compression rules for specific file types
  • php.ini access prevention
  • Directory indexing

301 Redirects (Moved Permanently)

If you ever need to change your website URL, or decide to change the URL of some of your pages etc, by moving pages to different sections of your site, then a 301 Redirect is absolutely essential. On rare occasions a website will need to change specific page URL’s, or even their entire domain and without an .htaccess file in place, they could lose valuable search engine rank because they didn’t inform the search engine of the move. A 301 redirect tells a search engine that the following page they were originally looking for has now moves permanently and the search engine should now go to the new URL and please index this new URL instead, all while removing the old URL from the search engines indexing. The outcome of this process takes a few days to possible weeks to occur, but the initial instruction that a page has moved is instant.

How do I know if I need to use a 301 Redirect?

If you’re ever in doubt, then the following example should give you a clear indication if you’re in need of a 301 Redirect.

Let’s say I had a page on my website that has been at the current URL for several days; the chances are that page has already been indexed by search engines, and if you had large volumes of traffic to that page, then it’s bound to have been shared on social media and the page URL is now out and about being shared by people all over the world. You then decide that you didn’t like where that page was located on your website and wanted to move it, but due to how your website is setup, moving the page to a different category or sub directory would change the URL. And example of this would be:

https://yourwebsite.com/blog/science/the-sun-is-really-bright

The bold part of the URL is actually where your page/post is located on your server, but you decide that you didn’t want that post to be within the /blog/science directory, but rather within /blog/nature. If you changed that page category by having its’ parent nature rather than science, then your new URL would be https://yourwebsite.com/blog/nature/the-sun-is-really-bright. The URL has changed completely, even though all you did was change the category, so a 301 Redirect will need to be in place to tell search engines that the old URL needs to now point to the new URL.

Where do I find the .htaccess file and how do I edit it?

If your server is setup correctly, your .htaccess file should be located directly within the main website directory. You can edit this .htaccess file by using either a built in editor through your hosting using cPanel or if you host your own files; you can download a free editor like Notepad ++. All of this you can find out from your web host provider or administrator.

Now that you’ve got your .htaccess open in an editor you can begin placing a 301 Redirect rule inside. All you need to do is paste the following, and change the parameters to meet your needs:

  • RewriteEngine On
  • RewriteBase /
  • RedirectMatch 301 /blog/nature/the-sun-is-really-bright /blog/nature/the-sun-is-really-bright

You need to make sure there’s a space between your old URL and the new URL even though they’re on the same line. The first two red lines are required only once with a redirect, and all your redirect URL’s can be directly beneath them. Provided that your Rewrite base is / (indicating home directory).

The first part of your redirect URL tells the search engine or user browser what type of redirect is about to occur. In the example above this is illustrated with the orange text. The next part of the redirect tells the search engine/browser what specific URL that needs to be redirected is the following, this is depicted in the green text. The blue text then tells the browser where it must go instead.

302 Redirects (Moved Temporarily)

A 302 Redirect works similarly to the 301 Redirect, with the exception that a 302 Redirect tells search engines that the redirect is only temporary and that the search engine should keep coming back to check. Whether a search engine actually handles 302’s properly is an open question. If a 302 is used instead of a 301, search engines might continue to index the old URL, and completely disregard new one as a duplicate. Using a 302 redirect is very rare, as how many times does a person need to temporarily move a web page? On many occasions a person will use a 302 instead of a 301 and vice versa; which is why we’ve helped you know the difference.

Sending
User Review
( votes)
Support Us By Sharing

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code