htaccess Code Command Samples to fix Your Website

In this post

Find 5 easy to use copy & paste r htaccess code samples that will help eliminate duplicate content and display your web site how you see fit.

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.

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

htaccess code

Redirect canonical URLs with htaccess code

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:

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.

Redirect 404 pages to a custom error page with htaccess code

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 “” with your actual URL and the page you want them to see. Viola.

How to remove file extensions with .htaccess

Are you looking to remove the file extent ion 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.

..look like this.

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.

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.

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

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.

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 unexperienced hands. Practice safe coding folks and back up, back up, back up your files first.



htaccess Code Command Samples to fix Your Website Click To Tweet