8 Great htaccess rules to improve your website

.htaccess files are incredibly useful and versatile – it allows us to do the following all from a single simple file that resides in the root directory of your website server.

You should be fairly skilled with Web development before meddeling with or creating .htaccess rules.

Benefits of using .htaccess rules

Enforcing or removing the www prefix in URLS

Prevent directory listings and viewing of .htaccess file

Handling custom 404 (File not found) error pages

Using cache control to improve Website performance

.htaccess can force download prompts for certain file types

Redirect from olddomain.com to newdomain.com

.htaccess temporary page re-direct during maintenance

SEO friendly .htaccess rules for single pages redirects

Does my host support .htaccess rules?

As a general rule, if your server runs Unix or Linux, or any version of the Apache web server it will support .htaccess rules, although your host may not allow you to use it.

What we are going to do

I will just be giving code examples and downloads that can be copied, pasted and tweaked to suit your needs as creating .htaccess files can be complicated.

.htaccess files can be edited in notepad or any basic text or developer program such as Dreamweaver, avoid Microsoft Word as it may save or input unwanted characters.

Backup your existing .htaccess file on your server before making changes, and note, changes made in your .htaccess file can stop your site from working properly or even totally, if it does – reverse your changes by restoring the backup file, or deleting the one you uploaded – problem solved!

Download one of the .htaccess files at the end of this article to re-use or tweak to suite your needs.

.htaccess rules to enforce or remove www prefix in URLS

First off, we comment code in a .htaccess file by starting a line with # the remainder of that line will not be processed, now lets wade into the code!

# Redirect all users to access the site WITHOUT the www. prefix
RewriteEngine on
rewritecond %{http_host} ^www.pgwebdesign.net [nc]
rewriterule ^(.*)$ http://pgwebdesign.net/$1 [r=301,nc]

This .htaccess rule re-writes all urls/pages accessed on this domain without the www prefix, 301 means permanent – if we used 307 it would be temporary, type in your address bar www.pgwebdesign.net and see it re-write it without the www.

As you can see I use this on my website, it keeps the URLS shorter and prevents duplicate content, while I link to all my urls without the www prefix, other website that link to me may use www, and by default the same page can be accessed with or without the prefix, for example http://example.com/hi/ and http://www.example.com/hi/ are pointing to the same page, but are considered separate by search engines, we want to prevent this.

You may also enforce using the www prefix (below), either rule has the same end result, it’s down to preference, if your unsure enforcing www can be the safer bet. Remember to change the domain name to your own.

# Redirect all users to access the site WITH the www. prefix
RewriteEngine on
rewritecond %{http_host} ^domain.com [NC]
rewriterule ^(.*)$ http://www.domain.com/$1 [R=301,nc]

Prevent directory listings and viewing of .htaccess file

To prevent users from poking around or reading our .htaccess rules, add the following into the .htaccess file.

# Disable directory listing from this point
Options -Indexes
# Prevent viewing of htaccess file
<Files ~ "^\.ht">
order allow,deny
deny from all
satisfy all
</Files>

Custom 404 (File not found) error pages

All websites or at the very least all medium-large websites should have a 404 page, the last thing a user needs to see when they land up on a page that has moved, no longer exists or is typed wrong is something like this:

Here are some resources for creating a usable and friendly 404 error page:

Redirect from olddomain.com to newdomain.com

Let’s say you have an old website that is accessible under olddomain.com and you have a new website that is accessible under newdomain.com. Copying the content of the old website to the new website is the first step, then
you should do a 301 moved permanently redirect from the old domain to the new domain, or a 307 if it’s temporary.

The advantages of doing this are:

Users will automatically be redirected to the new domain

Search engines will be redirected to the new domain

Google’s PageRankTM will be transferred to the new domain, in addition to internal information that is being used to set the position of pages in the search engine result pages (serp’s)

The Solution? – a 301 (permanent) redirect for all http requests that are going to the old domain, change newdomain.com to the relevant domains name, and place this file on your old domain.

Re-direct users to a temporary page during maintenance

Need to update some content that might disrupt users currently browsing the website? or you might have been hacked :( for this we redirect all users to a temporary page while maintenance is being performed, in this case I have made a file called maintenance.html and uploaded it to our root directory, whenever maintenance needs to be performed, add this rule to your .htaccess file and all users who access your website will be temporary re-directed to the maintenance page, this method is also SEO friendly.

SEO friendly redirects for single pages

Let’s say you have a page called http://mywebsite.com/na89s3.html, which you want to rename, but you don’t want to change it because users won’t be able to find it and all the search engines are pointing to it, no problem! we can do a permanent (301) or temporary (307) redirect, which automatically points users to the new page. (The new page still needs to exist at the new destination.)

of course such as your web-site nevertheless, you must analyze this transliteration on several of one’s discussions. Some of them are usually filled along with transliteration challenges and I locating the item incredibly disturbing to tell the fact then again I am going to unquestionably are available just as before all over again.

Your blog seems to be having some compatibilty issues in my opera browser. The text appears to be running off the webpage pretty bad. If you would like you can e-mail me at: and I will shoot you over a screen shot of the problem.

I have been exploring for a little bit for any high-quality articles or blog posts on this sort
of space . Exploring in Yahoo I ultimately stumbled upon this web site.
Studying this info So i am glad to show that I’ve
an incredibly excellent uncanny feeling I came upon just what I needed.
I most unquestionably will make certain to do not disregard this web site and
provides it a glance regularly.

Wonderful web site download teamviewer free software 4ustudy. Many useful info in this article. I’m giving them to a few associates ans furthermore sharing in delectable. Not to mention, appreciate your effort!

I do not know if it’s just me or if perhaps everyone else experiencing problems with your site.
It appears as though some of the written text within your content are running off the screen. Can someone else please comment and let
me know if this is happening to them too? This may be a problem with
my web browser because I’ve had this happen before.
Appreciate it

A fascinating discussion is definitely worth comment. I do think that you ought to publish more on this issue, it might
not be a taboo matter but generally people do not talk about such subjects.
To the next! Cheers!!

Excellent factors totally, you only been given a completely new target audience. Quantity you highly recommend relating to aplikasi berita saham terbaru your posting for you to created few days during the past? Any certain?