.htaccess: an example of the Apache per-directory configuration file
Using bloxsom as my weblog designing application, I had to provide some settings to the Apache
.htaccess configuration file to fine-tune the usage of my site:- tell Apache to load
index.cgiin addition to the defaultindex.htm - rewrite the requested address adding
www.if it's omitted - rewrite the requested address adding
index.cgiif it's omitted
- Tell Apache to load
index.cgiin addition to the defaultindex.htmwith theDirectoryIndexdirective:
DirectoryIndex index.cgi
- Rewrite the requested address adding
www.if it's omitted:
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\..* [NC] RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]Using themod_rewritemodule I tell Apache:- activate the rewrite engine:
RewriteEngine On
- what is the base address that must be rewritten; it means, what is the part of the address that was typed in the browser and must be rewritten:
RewriteBase /
If a user typeshttp://www.mycomputingart.com/something/, take the sub-path/something/and do what I'm going to tell you next on that part only. For instance, if my site was likehttp://www.somewhere.com/users/Z24/I would have done:RewriteBase /users/Z24/
meaning that the rewrite must be done on the sub-path under/users/Z24/.
Ifhttp://www.mycomputingart.com/users/Z24/linux/tutorial.htmwas requested, strip the/users/Z24/sub-path, resulting intolinux/tutorial.htm, reattach the FQDN (http://www.mycomputingart.com/linux/tutorial.htm) and do the rewrite.
It's usually not needed to map a URL path to a physical path in the.htaccessfile because most times it's done using theAliasdirective in thehttpd.confmain configuration file. For instance,Alias /users/Z24 /home/z24/www
would map the URL sub-path/users/Z24to the local directory/home/z24/www, so that the Apache server running on the machine located at the IP address corresponding to the domainwww.mycomputingart.comwould answer to the previous http request displaying the filetutorial.htmlocated at/home/z24/www/linux/. - now, examine the sub-path under the
RewriteBaselocation in the requested address and see if it satisfies this condition:
RewriteCond %{HTTP_HOST} !^www\..* [NC]TheRewriteConddirective lists the condition,%{HTTP_HOST}is a server variable identifying the fully qualified domain name (www.mycomputingart.com), the regular expression!^www\..*means "not (!) starting (^) with www. (www\.) and everything next (.*)" and[NC]means "no case" (consider WWW and www as equal). - when the condition is satisfied, rewrite the address:
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]TheRewriteRuledirective tells Apache what the address part identified by theRewriteConddirective must be replaced with: take everything (.*) from the begin (^) of the string identified byRewriteCond, store it in memory and replace it withhttp://www., then the content of the server variableHTTP_HOSTand then the content that has just been stored in memory ($1is the first pattern that was memorized; the pattern are stored in memory if they are enclosed by parenthesis:(.*)in this case).
The[R=301]tells Apache that the requested URL "has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs", as stated by the rfc 2616 301 error code.
- activate the rewrite engine:
- Rewrite the requested address adding
index.cgiif it's omitted; it should be done only if the requested URI is:- the root (
www.mycomputingart.com) - a single post (
www.mycomputingart.com/configurations/both/Z24.htaccess.html) - a category (
www.mycomputingart.com/configurations/)
I don't repeat theRewriteEngine On RewriteBase /
and the new rewrite rules are:RewriteCond %{REQUEST_URI} ^$|txt$|1993$|html$ [OR] RewriteCond %{REQUEST_URI} !\.[a-z]{3,4}$ [NC] RewriteRule !^index\.cgi.* - [C] RewriteRule (.*) index.cgi/$1- Agreeing with the first condition the requested URI (server variable
REQUEST_URI) must be either blank (^$) -- a location -- or (|) ending intxt,1993orhtml(txt$|1993$|html$) -- the bloxsom flavours. The[OR]means exactlyor, making this condition alternative to the next condition, which tells that the requested URI must not (!) end ($) with a file extension: a dot (\.) followed by 3 or 4 not case-sensitive ([NC]) alphabetic chars ([a-z]{3,4}).
In simpler words, the requested URI must be the root, a location, a file with an extension matching a bloxsom flavour or everything else having no extension. - If one of the two conditions is satisfied, check if the requested address does not begin (
!^) withindex.cgiand do nothing (-), then chain ([C]) this rule to the next.
Chaining two or more rules means that if the rule is not matched all the chained rules are skipped, if the rule is matched the next chained rule is processed. - The second
RewriteRuleattachesindex.cgiat the begin of theRewriteBasepath. In other words, it replaces everything ((.*)) after the / withindex.cgi/and the same everything ($1).
Combining the two rules, this is the result: if an address containing index.cgi has been requested, pass the requested address without rewriting; if it doesn't, rewrite the address adding index.cgi before any sub-path.
- the root (
<Files .htaccess> order allow,deny deny from all </Files>These rules tell Apache to deny access to the .htaccess file to everyone.
See the rewrite guide on the Apache site and the Perl Compatible Regular Expressions man page: they are enlightening.
Posted by: Z24 | Sun, Feb 11 2007 |
Category: /configurations/both |
Permanent link |
home
Tagged as: .htaccess, apache, bloxsom, configuration, web
http://www.mycomputingart.com/
To contact the webmaster and author write to: info<at>mycomputingart<dot>com
© mycomputingart.com, year(today()).


