If you’ve ever worked with .htaccess code files than you know they are one of the quickest ways to affect your entire site. The following .htaccess codes help fix some issues people commonly run into with their websites. If you’re looking to redirect a page or a portion of your website or if you’re interested in altering how your URL structure appears then you’ve found the right resource.

All I have to say before starting this post is make sure to back up your files before you start. Your .htaccess file is very powerful and can end up breaking your site if you don’t know what you’re doing.

Let’s get started with some handy bits of .htaccess code that will serve you well.

Redirect canonical URLs with htaccess Code Examples

This is another tip for avoiding dupe content by removing canonical URLs before they happen. Canonical URLs are URLs that appear more than once and can be indexed by the search engines; more than once, making them think you’re repeating your content in more than once place.

Example of canonical URLs:
www.yoururl.com
yoururl.com

The previous 2 URLs can be indexed more than once and once again can get you flagged for duplicate content. To avoid this apply the following code to your .htaccess file and the problem will go away.

RewriteCond %{HTTP_HOST} ^yoururl.com [NC]
RewriteRule ^(.*)$  [L,R=301]

Redirect 404 pages to a custom error page with htaccess Code Examples

htaccess

Custom error pages are a good way to redirect would be viewers to content on your website that they may also be interested in. Let’s face it if someone is looking for something specific and they don’t find what they want when they get to your website they’re likely to be a bit put out and bounce. Creating a custom error page for your site is an elegant way to entice users to stay right where they are and browse further. It’s suggested to use some sort of humor to help alleviate the fact that the person did not find what they were looking for. Help keep people on your page with the code below.

To add this bit of code to direct error pages to your own custom error page all you have to do is replace “www.yoururl.com” with your actual URL and the page you want them to see. Viola.

ErrorDocument 404 

htaccess Code Examples for Removing file Extensions

Are you looking to remove the file extension names of your pages? If so here’s a quick way of making your curls look that much friendlier. This will make pages that look like this.

www.yoururl.com/yourpage.php

..look like this.

www.yoururl.com/yourpage/

This way your URLs will be prettier and not only that more SEO friendly. The search engines don’t need to see whether you’re using PHP or asp or HTML. This will help to make your URL more exact to your targeted keywords and when Google’s happy you are happy.

Quick tip: if you’ve already started the rewrite engine earlier in your file you can omit “RewriteEngine On” as it’s already been started.

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.php !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
How to remove trailing slashes with .htaccess code

Removing the trailing slashes from your URLs ensures that your pages aren’t indexed more than one. This means that rather than the search engines seeing duplicate pages becomes a thing of the past. Duplicate content is bad and can end up getting you flagged for having the same content more than once. This can end up getting you blacklisted and is no good if you’re looking to get more traffic to your website. So rather than the bots seeing.

www.yoururl.com/yourpage/

..and.

www.yoururl.com/yourpage

..as 2 separate pages with the same content try this little bit of code.

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$  [L,R=301]
Redirect old pages with htaccess code

If you have a URL that is no longer available or if you’ve changed the structure of your URL you can tell the search engines where to find your new page or redirect them to another page of your choosing. This comes in particularly handy for WordPress sites when you move or change the name of a post. Take notice that this is good when you have just a few pages that have been moved, but if you change pages more often than this it’s not advised as you will have a ridiculously large .htaccess file. Redirect 301 /journal/ http://www.yoururl.com

Redirect 301 /journal/ http://www.yoururl.com

I’ve used all the following tips above on my own website in order to ensure that my sites behave how I want them to and don’t show up multiple times in the SERPs. Again your .htaccess file is the quickest way to alter your entire website which can be trouble in inexperienced hands. Practice safe coding folks and back up, back up, back up your files first.

Note: Make sure to back up your .htaccess file. It’s powerful and affects your entire site. It can break it if it’s not formatted correctly. Be safe. Keep a copy.