Hi,
.htaccess is a powerfull configuration file, where you can set and say many things very easily. It covers all the web application (directory with subdirectories) and the results are stunning - what takes you a day to create and debug in PHP, you can make by a two lines of regular expressions in .htaccess.
Basically, Apache has many modules, they are named mod_something. In .htaccess, you can config many of these modules (if it’s applicable for the scripts), but also Apache itself (some settings).
For example, you can specify a document, that will be shown, if the file is not found:
ErrorDocument 404 /404.html
One of the best known is mod_rewrite. It’s used for handling URLs, mainly for the "Cool URLs" phenomenon (see my other posts). General mod_rewrite syntax goes like this:
RewriteCond some-condition-here
RewriteCond another-condition-can-be-here
RewriteRule what-url-client-wants what-he-really-gets flags
So, basically, the whole thing is about: client wants someting, but we will give hime output of ANOTHER script and DON’T TELL HIM.
Note, that in all conditions and RewriteRule parameters can be used regular expressions. If you are familiar with regexps, you know, how powerfull i can be... RewriteCond and also RewriteRule has many parameters, sitches and built in variables. Such as your ip, cookies, http referrer - and all of these you can test in RewriteCond and do thing based on them...
MODx in default use this rule for creating cool urls:
"If client wants www.site.com/alias and this file/directory does not exist, we will run www.site.com/index.php?q=alias and give hin output of this script instead".
And how it is done? by three commands:
RewriteCond %{REQUEST_FILENAME} !-f - means: follower RewriteRule will be applicable only if the file, whose name is equal to requested filename does not exist ( !-f )
RewriteCond %{REQUEST_FILENAME} !-d - means the same with directory
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] -the main rule, lets have a look at it:
^(.*)$ - regular expression. ^=begin of a string, .=any character, *=unlimited repeat and $=end of a string. Characters ( and ) means nothing, they only specify a substring for further reference. So, ^(.*)$ matches the whole string (the whole http GET argument - what the browser wants to get (what file) ).
T index.php?q=$1 is a result, that the string that mathces will be converted to (it’s like a little bit complicated search-and-replace) and $1 means "the firs substring in ( )"... so it takes the whole string according to previous pattern, and puts it at the place of $1.
This is just a basics. Yo can have a look at
http://www.myhtaccess.com to see what you can do with .htaccess - the names of the trick will tell you.
I hope I helped =)
I made a big post somewhere here about SEO and the whole Cool URLS phenomen - basicly saying why cool urls are great and also mentioning some points that my .htaccess covers.
The experimental caching lines are really for nothing - I just wasn’t happy with how modx cache works and want do some test for future development.