Jens Oliver Meiert

.htaccess: 7 Useful Ingredients (Plus Bonus)

Post from April 12, 2007 (↻ December 12, 2016), filed under .

This and many other posts are also available as a pretty, well-behaved e-book: On Web Development.

Right on, here’ some helpful constituents of any decent .htaccess configuration file, based on my experience with quite a few lightweight projects. It’s a pretty simple collection and you’re surely aware of some of these things. However, this simpleness might come in handy once your provider is somewhat restrictive (as DreamHost, for example, even dislikes CheckSpelling).

  1. AddDefaultCharset On|Off|charset

    The AddDefaultCharset directive adds a default encoding parameter when the content type of a response is text/plain or text/html. (As far as I know the name of the directive is misleading—it specifies an encoding, not a character set.) It must be noted that when set, all affected files need to use the specified encoding. This directive also takes precedence over other encoding definitions.

    Example that makes utf-8 the default encoding: AddDefaultCharset utf-8.

  2. CheckSpelling On|Off

    CheckSpelling is a nice way towards a more “tolerant” website experience and to unburden error logs since it can be used to auto-correct file requests. When activated, this “spell checker” compares each document name in the requested directory against the requested document name without regard to case, and it allows up to one misspelling (character insertion, omission, transposition, or a wrong character).

    Example for CheckSpelling being enabled: CheckSpelling On.

  3. ContentDigest On|Off

    The ContentDigest directive enables Content-MD5 headers as specified in RFC 1864 and RFC 2068, respectively, and thus makes you benefit from caching. Please note that ContentDigest may influence server performance, though; also, there are alternatives.

    Example use: ContentDigest On.

  4. DefaultLanguage MIME-lang

    DefaultLanguage “tells Apache that all files in the directive’s scope (e.g., all files covered by the current <Directory> container) that don’t have an explicit language extension (such as .fr or .de as configured by AddLanguage) should be considered to be in the specified MIME-lang language”.

    Example that sets German as the default language: DefaultLanguage de.

  5. ErrorDocument error-code document

    The ErrorDocument directive’s certainly one of the most considerable directives to set due to the importance of error pages (errors occur but are no fun, so those documents should be to the point and helpful). The directive allows you to define specific documents for certain HTTP status codes.

    Example pointing to a “error404” document in case a beloved “not found” error shows up: ErrorDocument 404 /error404.

  6. Redirect [status] URL-path URL
    RedirectMatch [status] regex URL

    Redirect and RedirectMatch are other very important and quite popular directives, allowing, right, to redirect requests to alternative locations, be they temporary or permanent or whatever, as defined by the respective HTTP status code. While Redirect is pretty simple and surely way more popular that RedirectMatch, the latter means more possibilities for more elegant redirects. However, due to both popularity and complexity (of at least RedirectMatch’s regex stuff), I may just commend to take a closer look at the Apache documentation, and to do some tests.

    Examples permantly redirecting /count to a canonical Mint tracker as well as former GIF images to PNG:

    Redirect 301 /count http://example.com/mint/?js
    RedirectMatch 301 /img/(.*)gif http://example.com/img/$1png
  7. RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

    This is not a short description of a single directive you could use in your .htaccess file, but rather a snippet that’s for immediate use when it comes to that “canonical domain name with or without www” question. The above snippet rewrites URLs starting with “www.example.com” to “example.com”, and it’s easily changed so that it does so vice-versa. Although some claim that “www. is deprecated” (I tend to support that) and search engines like Google already allow you to specify the preferred domain spelling, that’s still a “matter of taste” that can be wired as shown anyway.

Bonus References

Some additional useful .htaccess related articles and tutorials on top. In alphabetical order.

About the Author

Jens Oliver Meiert, photo of July 27, 2015.

Jens Oliver Meiert is an author, developer (O’Reilly, W3C, ex-Google), and philosopher. He experiments with art and adventure. Here on meiert.com he shares and generalizes and exaggerates some of his thoughts and experiences.

There’s more Jens in the archives and at Goodreads. If you have any questions or concerns (or recommendations) about what he writes, leave a comment or a message.

Comments (Closed)

  1. On May 9, 2007, 17:10 CEST, Robert said:

    Hi Jens,

    I am not having any luck using:

    AddDefaultCharset utf-8

    on a Dreamhost server…

    Am I missing a special step for the Dreamhost world.

    Thanks

  2. On May 9, 2007, 19:53 CEST, Jens Oliver Meiert said:

    Robert, what do you mean by “not having any luck”?

    I just made sure I use AddDefaultCharset on WHWS (DreamHost hosted), and that works fine.

  3. On May 9, 2007, 22:19 CEST, Robert said:

    Hi Jens,

    On this page I have a meta tag for charset along with a one line .htaccess file:

    AddDefaultCharset utf-8

    When I check the response headers using the Firefox Web Developer Extension:

    Date: Wed, 09 May 2007 21:11:15 GMT
    Server: Apache/1.3.37 (Unix) mod_throttle/3.1.2 DAV/1.0.3 mod_fastcgi/2.4.2 mod_gzip/1.3.26.1a PHP/4.4.4 mod_ssl/2.8.22 OpenSSL/0.9.7e
    X-Powered-By: PHP/5.2.1
    MS-Author-Via: DAV
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: text/html

    200 OK

    The content-type is text/html not text/html; charset=utf-8

    :-) I enjoyed the WHWS!

    Robert

  4. On May 10, 2007, 9:54 CEST, Jens Oliver Meiert said:

    I probably have to look up some things again, but as far as I see, the reply you show here is based on a HEAD request. A GET will reveal that the encoding is set to UTF-8, though via meta element.

    AddDefaultCharset’s definitely applied when there is no encoding information available (see example text file via HEAD request). While you might want to check what happens when you don’t set the encoding via meta (not always advisable though), I might take a look at the specs again.

  5. On December 12, 2008, 15:37 CET, MrBruks said:

    thanks for links Meiert, I’ve just solved my problem with .htaccess

Read More

Have a look at the most popular posts, possibly including:

Or maybe say hi on Twitter, Google+, or LinkedIn?

Looking for a way to comment? Comments have been disabled, unfortunately.

Flattr? Found a mistake? Email me, jens@meiert.com.

You are here: HomeArchive2007 → .htaccess: 7 Useful Ingredients (Plus Bonus)

Last update: December 12, 2016

“The end does not justify the means.”