If you have a website, it is most likely that your server is using Apache as its web server software. Apache has a nice feature that provides the ability to customize configuration directives defined in the main configuration file using a file called .htaccess. In this article I'd like to discuss how you can use the .htaccess file to pimp up your website.
Lets start with my personal favorite: mod_rewrite. It's an extension module that allows you to mask URIs. An example: I have the URI http://jero.net/contact to point to my contact form, but there's no file called contact. The URI to my contact form points to an ugly WordPress generated URI with some PHP variables. This can be done with the following configuration:
RewriteEngine On
RewriteRule ^contact/?$ /wordpress/index.php?name=$1
I also use mod_rewrite to redirect anyone who accesses this website with the www subdomain to the bare domain name, because the www subdomain is just pointless as described on no-www.org. Here's the configuration for .htaccess to achieve this effect (this code can also be found at no-www.org):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
If you'd like to learn more about this module, do to Apache's documentation about mod_rewrite.
Using this is absolutely a must. You can allow the server to compress the HTML using gzip and send it to the browser (if it supports gzip, unlike IE). The browser receives it, decompresses it and shows the page like it would normally do. This will definitely reduce your bandwidth. The front page of this website was around the 10KB before I used gzip to compress my pages, and now it is around 3,50KB. mozilla.org has a nice page for a more detailed description about gzip compression. Hereâ??s the configuration to use gzip compression for your HTML files: (Yes, it's just one line.)
php_value output_handler ob_gzhandler
AddDefaultCharset directiveHow does a user agent know which character encoding has been used? The server should provide this information. The most straightforward way for a server to inform the user agent about the character encoding of the document is to use the "charset" parameter of the "Content-Type" header field of the HTTP protocol ([RFC2616], sections 3.4 and 14.17).
That's what the HTML 4.01 Specification tells us about defining the
character encoding. Yet, many people still use the meta element for this purpose. The meta element also sends an HTTP header, but when the browser
parses the document, it has to guess character encoding to parse the beginning
of the document because the character encoding is only defined later on, in the
head element. So it\ s much better to
configure your server to send the HTTP header before the file is being parsed.
You can use the .htaccess file for this as well, with the
AddDefaultCharset directive. The configuration below configures
the server to send all documents with the UTF-8 encoding:
AddDefaultCharset utf-8
Learn more about the AddDefaultCharset
directive.
ErrorDocument directiveI guess this is probably the most well known directive, but I'd like to put
it in this article anyway. With this directive you can specify what file should
be shown when a certain HTTP Status Code has been sent. So if a user agent
tries to access a file on a server that does not exist, the server responds
with the 404 Status Code. You can use the ErrorDocument directive
to show a custom error page instead of Apache's boring default page. Here's an
example configuration to define a custom error page for when a 404 error code
has been sent:
ErrorDocument 404 /error-docs/404.html
Get more info about the ErrorDocument
directive.
Those were the directives I currently use in my .htaccess file. But of course, there are a lot more. Most of them are explained at the page Apache Core Features. If you have any other .htaccess tricks that you find very useful, feel free to post them.
Copyright © 2005 - 2007 Jeroen van der Meer. All rights reserved.