The Little Hidden Treasure File Called .htaccess - Montana Webmaster

The Little Hidden Treasure File Called .htaccess

The .htaccess file contains lines of code called server directives.

What a Web Server Does

Photo of server farm
Your website may live in a server farm

The pages of your website are stored on your web host, waiting for shipping to a viewer’s computer. The web servers in your hosting plan put together your web pages, package them up and ship them out to viewers who ask for them. They follow a very specific set of instructions from the web server software and the software, such as WordPress, that runs your website.

About 1/3 of web servers use Apache software. The .htaccess is only available in Apache. Other systems have other methods to achieve the same outcomes.

Special Instructions for the Website Packaging and Shipping Department

But, what if you have a special situation where you want the server to do something different from the regular instructions? Here are some examples:

1. Scenario 1: You reorganized your site, and some … or many … of your pages have new addresses. People out there are going to click links, from Google and other sites, that go to the old address. Normally, your web server would just send them an “OOOOPS! That page doesn’t exist” message. You need a way to tell the server, if people ask for this address, send them to the new address instead.

2. Scenario 2: You have a secure certificate for your website, so your addresses start with https://, instead of http://. But you know that some people will get it wrong and go to http://. To maintain security, you want the server to show https:// no matter what address the viewer uses.

In these situations, you want the web server to make a change in its normal process. To do this, we often use a .htaccess file.

What Does the .htaccess Do?

When You Change the Address or Name of your Website Pages

There are two different types of code you can use: a redirect or a rewrite. If your new links, compared to your old links are in a very predictable pattern, you can use a Rewrite Rule. For example, if you change the name of a folder on your website and the folder is part of the address, you need to let the server know that anyone who uses the old folder name should see the files in the new folder. If all the file names are the same, you can use code to go through the whole folder and just change the folder part of the address. But, if each of the files also now has a new name that does not have a predictable pattern, you will have to write a line of code for each file using 301 redirects.

The 301 Redirect has 3 parts:

  1. Redirect 301
  2. The old file address
  3. The new file address

Example: Redirect 301 /mulch/ http://rawlingsmanufacturing.com/applications/compost-and-mulch-facilities. In this case, we made the file name communicate more of what the page is about.

When You Change the Address for SSL

In Scenario 2, you can use a Rewrite Rule … all requests that come in as http will show in the browser address bar as https and will be routed through the secure server. There is no variation. This part of a URL is called a protocol. It says that this address is for a web page and should be opened in a browser. https is only different from http in that it has a secure certificate attached to it. One address change rule will be correct for all the pages on your website.

There are some variations available, but here is the basic code:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

All that code: ^(.*)$ https://www.yourdomain.com/$1 [R,L] is called regular expressions. This kind of code is not a programming language, instead, it does one specific job. It finds, or determines specific patterns in text. In this case, it makes sure that all the addresses to your site have https:// in them the address bar.

  • ^ means the string or line starting place
  • () is an expression. In this case, the expression is .*
  • $ marks the end of the string or line

In this case, ^(.*)$ means that there is no constraint on how long the text is that is being worked on. This is important because URLs can be very long.

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.