Tips for redirecting web pages via .htaccess document

I am constantly trying to keep my informational website up to standards and have decided to fix a few things. First I purchase and install a Secure Socket Layer which helps keep information safe and search-engines happy. This requires some work -- including adding a redirect to the code on my website's .htaccess file.

Next I work on giving users a better experience. Combining several 500-word pages into one 1500-word page gives my visitors more information, simplifies navigation, and helps the search engines do a better job. Of course, more redirects must be added to the .htaccess file so users don't get a "page not found" error.

There is also the matter of the Google Analytics notice about redundant host names that has been sitting in my account for weeks. Something must be done about that -- a third redirect is hopefully the answer.

Website navigation chart
Story board navigation diagram -- photo courtesy of Pexels.com

Keep your website up to standards with upgrades and redirects.

 


Redirecting visitors to the right webpages

Arrows point the way

Redirecting from http to https

After installing a secure socket layer on my website, I redirect everything to https instead of http. This is just a few lines of code added to my .htaccess file which is located in my site's root folder. So far so good.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Visit htaccess-guide.com for more info about .httaccess files.

I leave the text editor open because I am not done with it yet.

Redirecting an old page to a new one

Since some of my content has been combined and old pages deleted, I need to tell the search engines and browsers where to find the new and better content. A redirect is required for each "old" page. See examples below:

Redirect 301 /anxiety.html https://www.everygreenherb.com/stress.html
Redirect 301 /acne.html https://www.everygreenherb.com/skinProblems.html
Redirect 301 /eczema.html https://www.everygreenherb.com/skinProblems.html

Redirecting to my main hostname to avoid redundancy

Google Analytics sent me a notice weeks ago about redundant hostnames but I wasn't sure what to do. After some research, a redirect seemed like the easiest option.

The Analytics notice said, "Property https://www.example.com is receiving data from redundant hostnames. Some of the redundant hostnames are:

  • example.com
  • www.example.com

Redundant hostnames are counted as separate rows in reports, so hits that are going to the same page on your site from different hostnames will be split into multiple rows. With data split across multiple rows, traffic to specific pages will appear lower than it actually is. To avoid this problem, consider setting up a 301 redirect from one of your redundant hostnames to the other, or create a search-and-replace filter that strips 'www.' from hostnames."

Thanks, to Fast Domain for the explanation and 301-redirect code examples. Since I want to track my domain with the www I added this code to the .htaccess doc:

 
RewriteEngine On
RewriteCond %{HTTP_HOST} ^everygreenherb.com [NC]
RewriteRule ^(.*)$ http://www.everygreenherb.com/$1 [L,R=301]

My .htaccess document for everygreenherb.com ends up looking like this:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^everygreenherb.com [NC]
RewriteRule ^(.*)$ http://www.everygreenherb.com/$1 [L,R=301]

ErrorDocument 404 https://www.everygreenherb.com/404.html

Redirect 301 /anxiety.html https://www.everygreenherb.com/stress.html
Redirect 301 /acne.html https://www.everygreenherb.com/skinProblems.html
Redirect 301 /eczema.html https://www.everygreenherb.com/skinProblems.html

The 7th line of code is a common redirect that helps my visitors know what to do if one of my pages doesn't load in their browser. It gives them a chance to go to my homepage or to use my main navigation bar.

The Web Design Society celebrates originality, insists on good content, and craves eye-candy. For a professional website design, contact one of our web developers today.

© 2005-2018 by