.htaccess: 7 Useful Ingredients (Plus Bonus)
Published on Apr 12, 2007 (updated Dec 7, 2024), filed under development. (Share this on Mastodon or Bluesky?)
This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.
This article features useful directives for .htaccess configuration files, based on my experience with a number of lightweight projects. It’s a pretty simple collection and you may be aware of some of these things. However, the simplicity can come in handy when your provider is somewhat restrictive (as DreamHost, for example, dislikes CheckSpelling).
AddDefaultCharset On|Off|charsetThe
AddDefaultCharsetdirective adds a default encoding parameter when the content type of a response istext/plainortext/html. (As far as I know the name of the directive is misleading—it specifies an encoding, not a character set.) When set, the affected files use the specified encoding. This directive also takes precedence over other encoding definitions.Example that makes
utf-8the default encoding:AddDefaultCharset utf-8.CheckSpelling On|OffCheckSpellingis a nice way towards a better website experience. It can also unburden error logs since it auto-corrects file requests. When activated, the directive compares each document name in the respective directory against the requested document name without regard to case, and compensates for one misspelling (character insertion, omission, transposition, or otherwise wrong character).Example of
CheckSpellingbeing enabled:CheckSpelling On.ContentDigest On|OffThe
ContentDigestdirective enablesContent-MD5headers as specified in RFC 1864 and RFC 2068, respectively, and thus helps you with caching.ContentDigestcan impact server performance, though; also, there are alternatives.Example use:
ContentDigest On.DefaultLanguage MIME-langDefaultLanguage“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.fror.deas configured byAddLanguage) should be considered to be in the specified MIME-lang language.”Example that sets German as the default language:
DefaultLanguage de.ErrorDocument error-code documentThe
ErrorDocumentdirective is one of the most relevant directives to set due to the importance of error pages (as errors are bad for the user experience, those documents should be on point). The directive allows you to define documents for certain HTTP status codes.Example pointing to an “error404” document in case a “not found” error shows up:
ErrorDocument 404 /error404.Redirect [status] URL-path URL RedirectMatch [status] regex URLRedirectandRedirectMatchare other important and popular directives, allowing to redirect requests to alternative locations, be they temporary or permanent, as defined by the respective HTTP status code. WhileRedirectis pretty simple and probably more popular thatRedirectMatch, the latter means more options for more powerful redirects. However, due to both popularity and complexity (ofRedirectMatch’s regex capabilities), I recommend taking a closer look at the Apache documentation, and doing some tests first.Examples permantly redirecting
/countto a canonical Mint tracker as well as GIF images to PNG:Redirect 301 /count https://example.com/mint/?js RedirectMatch 301 /img/(.*)gif https://example.com/img/$1pngRewriteEngine on RewriteCond %{HTTP_HOST} ^www.example.com [NC] RewriteRule ^(.*)$ https://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 the “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 second 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 enforced as shown.
Bonus
I like to put some useful .htaccess articles and tutorials on top. In alphabetical order:
- Apache HTTP Server Project: Apache HTTP Server Tutorial: .htaccess Files;
- AskApache: Ultimate Htaccess Tutorial for .htaccess Files;
- Bently: More .htaccess Tips and Tricks;
- Esteves: .htaccess Cheatsheet;
- JavaScript Kit: Comprehensive Guide to .htaccess.
About Me
I’m Jens (long: Jens Oliver Meiert), and I’m a web developer, manager, and author. I’ve been working as a technical lead and engineering manager for companies you’ve never heard of and companies you use every day, I’m an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for O’Reilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)
