Some Good Official Tutorials
- Apache Docs Tutorial: .htaccess files
- Apache Docs: List of available Directives
- Apache Docs Tutorial: mod_rewrite Reference
- Apache Wiki: Various configuration recipes
Sample .htaccess file from htaccess Google Group
Sample .htaccess file from htaccess Google Group
Are you an advanced mod_rewrite expert or guru? This article is for YOU too!
The following undocumented techniques and methods will allow you to utilize mod_rewrite at an “expert level” by showing you how to unlock its secrets.
Most if not all web developers and server administrators struggle with Apache mod_rewrite. It’s very tough and only gets a little easier with practice. Until Now! Get ready to explode your learning curve,….
Decoding Mod_Rewrite Variables
So when I realized my problem was that I didn’t know the value of the variable being tested by the RewriteCond, I set out to try and discover how to view those variables.. Keep in mind you can also use RewriteLogging, but its only allowed for root users who can edit the httpd.conf, this is .htaccess.
Setting Environment Variables with RewriteRule
I discovered a multitude of methods to set and view apache environment variables, using various modules and some core tricks, but the method that allows me to view the most environment variables is RewriteRule.. I wanted to use SetEnvIf more, but its just not as powerful as mod_rewrite, due to programming.
This code sets the variable
INFO_REQUEST_URIto have the value ofREQUEST_URI.RewriteEngine On RewriteBase / RewriteRule .* - [E=INFO_REQUEST_URI:%{REQUEST_URI},NE]Saving the Apache Variable Values
Now the trick is how to view that environment variable… The method I came up with is nice… We will send the environment variable value in an HTTP Header, as there isn’t much data manipulation/validation so you get an accurate look at the actual value.. At first I tried adding the variable value to a redirection using the query_string.. but a HTTP_USER_AGENT value doesn’t play well as a query_string.
Using RequestHeader in .htaccess
This code takes advantage of the incredible mod_headers apache module to actually ADD a whole new header to YOUR request. Seriously one of the coolest tricks I’ve found yet.. Its almost the same as being able to spoof POST requests! Since Headers can be protected data… especially the HTTP_COOKIE header..
RequestHeader set INFO_REQUEST_URI "%{INFO_REQUEST_URI}e"Viewing the Variable Values
Now you can use any kind of server-run interpreter like perl, php, ruby, etc., to view all the variable values. All cgi-script handlers like those are able to view request headers..
PHP Code to access Apache Variables
Works even in safe-mode… any interpreter can view HTTP Headers! Note that each of these variables are added as HTTP headers to the request for the script.. kinda confusing.. So each variable sent as a header is prefixed with HTTP_ to denote it was a header.
<?php header("Content-Type: text/plain"); $INFO=$MISS=array(); foreach($_SERVER as $v=>$r) { if(substr($v,0,9)=='HTTP_INFO') { if(!empty($r))$INFO[substr($v,10)]=$r; else $MISS[substr($v,10)]=$r; } } /* thanks Mike! */ ksort($INFO); ksort($MISS); ksort($_SERVER); echo "Received These Variables:\n"; print_r($INFO); echo "Missed These Variables:\n"; print_r($MISS); echo "ALL Variables:\n"; print_r($_SERVER); ?>Time to Get Crazy
Just create the above php file on your site as
/test/index.phpor whatever, then create /test/.htaccess which should contain the below.htaccess filesnippet. Now just request/test/index.phpand be amazed!Ok, so I’ve prepared the .htaccess code you can use to view the values of all these variables. Just add it to a .htaccess file and make a request. For this test I created an index.php file that printed out all the
$_SERVERvariables, and made requests to it.RewriteEngine On RewriteBase / RewriteRule .* - [E=INFO_API_VERSION:%{API_VERSION},NE] RewriteRule .* - [E=INFO_AUTH_TYPE:%{AUTH_TYPE},NE] RewriteRule .* - [E=INFO_CONTENT_LENGTH:%{CONTENT_LENGTH},NE] RewriteRule .* - [E=INFO_CONTENT_TYPE:%{CONTENT_TYPE},NE] RewriteRule .* - [E=INFO_DOCUMENT_ROOT:%{DOCUMENT_ROOT},NE] RewriteRule .* - [E=INFO_GATEWAY_INTERFACE:%{GATEWAY_INTERFACE},NE] RewriteRule .* - [E=INFO_HTTPS:%{HTTPS},NE] RewriteRule .* - [E=INFO_HTTP_ACCEPT:%{HTTP_ACCEPT},NE] RewriteRule .* - [E=INFO_HTTP_ACCEPT_CHARSET:%{HTTP_ACCEPT_CHARSET},NE] RewriteRule .* - [E=INFO_HTTP_ACCEPT_ENCODING:%{HTTP_ACCEPT_ENCODING},NE] RewriteRule .* - [E=INFO_HTTP_ACCEPT_LANGUAGE:%{HTTP_ACCEPT_LANGUAGE},NE] RewriteRule .* - [E=INFO_HTTP_CACHE_CONTROL:%{HTTP_CACHE_CONTROL},NE] RewriteRule .* - [E=INFO_HTTP_CONNECTION:%{HTTP_CONNECTION},NE] RewriteRule .* - [E=INFO_HTTP_COOKIE:%{HTTP_COOKIE},NE] RewriteRule .* - [E=INFO_HTTP_FORWARDED:%{HTTP_FORWARDED},NE] RewriteRule .* - [E=INFO_HTTP_HOST:%{HTTP_HOST},NE] RewriteRule .* - [E=INFO_HTTP_KEEP_ALIVE:%{HTTP_KEEP_ALIVE},NE] RewriteRule .* - [E=INFO_HTTP_MOD_SECURITY_MESSAGE:%{HTTP_MOD_SECURITY_MESSAGE},NE] RewriteRule .* - [E=INFO_HTTP_PROXY_CONNECTION:%{HTTP_PROXY_CONNECTION},NE] RewriteRule .* - [E=INFO_HTTP_REFERER:%{HTTP_REFERER},NE] RewriteRule .* - [E=INFO_HTTP_USER_AGENT:%{HTTP_USER_AGENT},NE] RewriteRule .* - [E=INFO_IS_SUBREQ:%{IS_SUBREQ},NE] RewriteRule .* - [E=INFO_ORIG_PATH_INFO:%{ORIG_PATH_INFO},NE] RewriteRule .* - [E=INFO_ORIG_PATH_TRANSLATED:%{ORIG_PATH_TRANSLATED},NE] RewriteRule .* - [E=INFO_ORIG_SCRIPT_FILENAME:%{ORIG_SCRIPT_FILENAME},NE] RewriteRule .* - [E=INFO_ORIG_SCRIPT_NAME:%{ORIG_SCRIPT_NAME},NE] RewriteRule .* - [E=INFO_PATH:%{PATH},NE] RewriteRule .* - [E=INFO_PATH_INFO:%{PATH_INFO},NE] RewriteRule .* - [E=INFO_PHP_SELF:%{PHP_SELF},NE] RewriteRule .* - [E=INFO_QUERY_STRING:%{QUERY_STRING},NE] RewriteRule .* - [E=INFO_REDIRECT_QUERY_STRING:%{REDIRECT_QUERY_STRING},NE] RewriteRule .* - [E=INFO_REDIRECT_REMOTE_USER:%{REDIRECT_REMOTE_USER},NE] RewriteRule .* - [E=INFO_REDIRECT_STATUS:%{REDIRECT_STATUS},NE] RewriteRule .* - [E=INFO_REDIRECT_URL:%{REDIRECT_URL},NE] RewriteRule .* - [E=INFO_REMOTE_ADDR:%{REMOTE_ADDR},NE] RewriteRule .* - [E=INFO_REMOTE_HOST:%{REMOTE_HOST},NE] RewriteRule .* - [E=INFO_REMOTE_IDENT:%{REMOTE_IDENT},NE] RewriteRule .* - [E=INFO_REMOTE_PORT:%{REMOTE_PORT},NE] RewriteRule .* - [E=INFO_REMOTE_USER:%{REMOTE_USER},NE] RewriteRule .* - [E=INFO_REQUEST_FILENAME:%{REQUEST_FILENAME},NE] RewriteRule .* - [E=INFO_REQUEST_METHOD:%{REQUEST_METHOD},NE] RewriteRule .* - [E=INFO_REQUEST_TIME:%{REQUEST_TIME},NE] RewriteRule .* - [E=INFO_REQUEST_URI:%{REQUEST_URI},NE] RewriteRule .* - [E=INFO_SCRIPT_FILENAME:%{SCRIPT_FILENAME},NE] RewriteRule .* - [E=INFO_SCRIPT_GROUP:%{SCRIPT_GROUP},NE] RewriteRule .* - [E=INFO_SCRIPT_NAME:%{SCRIPT_NAME},NE] RewriteRule .* - [E=INFO_SCRIPT_URI:%{SCRIPT_URI},NE] RewriteRule .* - [E=INFO_SCRIPT_URL:%{SCRIPT_URL},NE] RewriteRule .* - [E=INFO_SCRIPT_USER:%{SCRIPT_USER},NE] RewriteRule .* - [E=INFO_SERVER_ADDR:%{SERVER_ADDR},NE] RewriteRule .* - [E=INFO_SERVER_ADMIN:%{SERVER_ADMIN},NE] RewriteRule .* - [E=INFO_SERVER_NAME:%{SERVER_NAME},NE] RewriteRule .* - [E=INFO_SERVER_PORT:%{SERVER_PORT},NE] RewriteRule .* - [E=INFO_SERVER_PROTOCOL:%{SERVER_PROTOCOL},NE] RewriteRule .* - [E=INFO_SERVER_SIGNATURE:%{SERVER_SIGNATURE},NE] RewriteRule .* - [E=INFO_SERVER_SOFTWARE:%{SERVER_SOFTWARE},NE] RewriteRule .* - [E=INFO_THE_REQUEST:%{THE_REQUEST},NE] RewriteRule .* - [E=INFO_TIME:%{TIME},NE] RewriteRule .* - [E=INFO_TIME_DAY:%{TIME_DAY},NE] RewriteRule .* - [E=INFO_TIME_HOUR:%{TIME_HOUR},NE] RewriteRule .* - [E=INFO_TIME_MIN:%{TIME_MIN},NE] RewriteRule .* - [E=INFO_TIME_MON:%{TIME_MON},NE] RewriteRule .* - [E=INFO_TIME_SEC:%{TIME_SEC},NE] RewriteRule .* - [E=INFO_TIME_WDAY:%{TIME_WDAY},NE] RewriteRule .* - [E=INFO_TIME_YEAR:%{TIME_YEAR},NE] RewriteRule .* - [E=INFO_TZ:%{TZ},NE] RewriteRule .* - [E=INFO_UNIQUE_ID:%{UNIQUE_ID},NE] RequestHeader set INFO_API_VERSION "%{INFO_API_VERSION}e" RequestHeader set INFO_AUTH_TYPE "%{INFO_AUTH_TYPE}e" RequestHeader set INFO_CONTENT_LENGTH "%{INFO_CONTENT_LENGTH}e" RequestHeader set INFO_CONTENT_TYPE "%{INFO_CONTENT_TYPE}e" RequestHeader set INFO_DOCUMENT_ROOT "%{INFO_DOCUMENT_ROOT}e" RequestHeader set INFO_GATEWAY_INTERFACE "%{INFO_GATEWAY_INTERFACE}e" RequestHeader set INFO_HTTPS "%{INFO_HTTPS}e" RequestHeader set INFO_HTTP_ACCEPT "%{INFO_HTTP_ACCEPT}e" RequestHeader set INFO_HTTP_ACCEPT_CHARSET "%{INFO_HTTP_ACCEPT_CHARSET}e" RequestHeader set INFO_HTTP_ACCEPT_ENCODING "%{INFO_HTTP_ACCEPT_ENCODING}e" RequestHeader set INFO_HTTP_ACCEPT_LANGUAGE "%{INFO_HTTP_ACCEPT_LANGUAGE}e" RequestHeader set INFO_HTTP_CACHE_CONTROL "%{INFO_HTTP_CACHE_CONTROL}e" RequestHeader set INFO_HTTP_CONNECTION "%{INFO_HTTP_CONNECTION}e" RequestHeader set INFO_HTTP_COOKIE "%{INFO_HTTP_COOKIE}e" RequestHeader set INFO_HTTP_FORWARDED "%{INFO_HTTP_FORWARDED}e" RequestHeader set INFO_HTTP_HOST "%{INFO_HTTP_HOST}e" RequestHeader set INFO_HTTP_KEEP_ALIVE "%{INFO_HTTP_KEEP_ALIVE}e" RequestHeader set INFO_HTTP_MOD_SECURITY_MESSAGE "%{INFO_HTTP_MOD_SECURITY_MESSAGE}e" RequestHeader set INFO_HTTP_PROXY_CONNECTION "%{INFO_HTTP_PROXY_CONNECTION}e" RequestHeader set INFO_HTTP_REFERER "%{INFO_HTTP_REFERER}e" RequestHeader set INFO_HTTP_USER_AGENT "%{INFO_HTTP_USER_AGENT}e" RequestHeader set INFO_IS_SUBREQ "%{INFO_IS_SUBREQ}e" RequestHeader set INFO_ORIG_PATH_INFO "%{INFO_ORIG_PATH_INFO}e" RequestHeader set INFO_ORIG_PATH_TRANSLATED "%{INFO_ORIG_PATH_TRANSLATED}e" RequestHeader set INFO_ORIG_SCRIPT_FILENAME "%{INFO_ORIG_SCRIPT_FILENAME}e" RequestHeader set INFO_ORIG_SCRIPT_NAME "%{INFO_ORIG_SCRIPT_NAME}e" RequestHeader set INFO_PATH "%{INFO_PATH}e" RequestHeader set INFO_PATH_INFO "%{INFO_PATH_INFO}e" RequestHeader set INFO_PHP_SELF "%{INFO_PHP_SELF}e" RequestHeader set INFO_QUERY_STRING "%{INFO_QUERY_STRING}e" RequestHeader set INFO_REDIRECT_QUERY_STRING "%{INFO_REDIRECT_QUERY_STRING}e" RequestHeader set INFO_REDIRECT_REMOTE_USER "%{INFO_REDIRECT_REMOTE_USER}e" RequestHeader set INFO_REDIRECT_STATUS "%{INFO_REDIRECT_STATUS}e" RequestHeader set INFO_REDIRECT_URL "%{INFO_REDIRECT_URL}e" RequestHeader set INFO_REMOTE_ADDR "%{INFO_REMOTE_ADDR}e" RequestHeader set INFO_REMOTE_HOST "%{INFO_REMOTE_HOST}e" RequestHeader set INFO_REMOTE_IDENT "%{INFO_REMOTE_IDENT}e" RequestHeader set INFO_REMOTE_PORT "%{INFO_REMOTE_PORT}e" RequestHeader set INFO_REMOTE_USER "%{INFO_REMOTE_USER}e" RequestHeader set INFO_REQUEST_FILENAME "%{INFO_REQUEST_FILENAME}e" RequestHeader set INFO_REQUEST_METHOD "%{INFO_REQUEST_METHOD}e" RequestHeader set INFO_REQUEST_TIME "%{INFO_REQUEST_TIME}e" RequestHeader set INFO_REQUEST_URI "%{INFO_REQUEST_URI}e" RequestHeader set INFO_SCRIPT_FILENAME "%{INFO_SCRIPT_FILENAME}e" RequestHeader set INFO_SCRIPT_GROUP "%{INFO_SCRIPT_GROUP}e" RequestHeader set INFO_SCRIPT_NAME "%{INFO_SCRIPT_NAME}e" RequestHeader set INFO_SCRIPT_URI "%{INFO_SCRIPT_URI}e" RequestHeader set INFO_SCRIPT_URL "%{INFO_SCRIPT_URL}e" RequestHeader set INFO_SCRIPT_USER "%{INFO_SCRIPT_USER}e" RequestHeader set INFO_SERVER_ADDR "%{INFO_SERVER_ADDR}e" RequestHeader set INFO_SERVER_ADMIN "%{INFO_SERVER_ADMIN}e" RequestHeader set INFO_SERVER_NAME "%{INFO_SERVER_NAME}e" RequestHeader set INFO_SERVER_PORT "%{INFO_SERVER_PORT}e" RequestHeader set INFO_SERVER_PROTOCOL "%{INFO_SERVER_PROTOCOL}e" RequestHeader set INFO_SERVER_SIGNATURE "%{INFO_SERVER_SIGNATURE}e" RequestHeader set INFO_SERVER_SOFTWARE "%{INFO_SERVER_SOFTWARE}e" RequestHeader set INFO_THE_REQUEST "%{INFO_THE_REQUEST}e" RequestHeader set INFO_TIME "%{INFO_TIME}e" RequestHeader set INFO_TIME_DAY "%{INFO_TIME_DAY}e" RequestHeader set INFO_TIME_HOUR "%{INFO_TIME_HOUR}e" RequestHeader set INFO_TIME_MIN "%{INFO_TIME_MIN}e" RequestHeader set INFO_TIME_MON "%{INFO_TIME_MON}e" RequestHeader set INFO_TIME_SEC "%{INFO_TIME_SEC}e" RequestHeader set INFO_TIME_WDAY "%{INFO_TIME_WDAY}e" RequestHeader set INFO_TIME_YEAR "%{INFO_TIME_YEAR}e" RequestHeader set INFO_TZ "%{INFO_TZ}e" RequestHeader set INFO_UNIQUE_ID "%{INFO_UNIQUE_ID}e"Mod_Rewrite Variables Decoded!
[API_VERSION] => 20020903:12 [AUTH_TYPE] => Digest [DOCUMENT_ROOT] => /home/user/www_root/askapache.com [HTTPS] => off [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 [HTTP_COOKIE] => PHPSESSID=752ee6d56e15f305233e30045987e5ce568c034; __qca=1176541225-59967328-5223185; [HTTP_HOST] => www.askapache.com [HTTP_REFERER] => http://www.askapache.com/protest/index.php?askapache=awesomeness&you=rock [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16 [IS_SUBREQ] => false [QUERY_STRING] => e=404 [REMOTE_ADDR] => 22.162.144.211 [REMOTE_HOST] => 22.162.144.211 [REMOTE_PORT] => 4511 [REMOTE_USER] => administrator [REQUEST_FILENAME] => /home/user/www_root/askapache.com/protest/index.php [REQUEST_METHOD] => GET [REQUEST_URI] => /protest/index.php [SCRIPT_FILENAME] => /home/user/www_root/askapache.com/protest/index.php [SCRIPT_GROUP] => daemonu [SCRIPT_USER] => askapache [SERVER_ADDR] => 208.113.134.190 [SERVER_ADMIN] => webmaster@askapache.com [SERVER_NAME] => www.askapache.com [SERVER_PORT] => 80 [SERVER_PROTOCOL] => HTTP/1.1 [SERVER_SOFTWARE] => Apache/2.0.61 (Unix) PHP/4.4.7 mod_ssl/2.0.61 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 [THE_REQUEST] => GET /protest/adf HTTP/1.1 [TIME] => 20080820014309 [TIME_DAY] => 20 [TIME_HOUR] => 01 [TIME_MIN] => 43 [TIME_MON] => 08 [TIME_SEC] => 09 [TIME_WDAY] => 3 [TIME_YEAR] => 2008Request using HTTPS
[API_VERSION] => 20020903:12 [AUTH_TYPE] => Digest [DOCUMENT_ROOT] => /home/user/www_root/askapache.com [HTTPS] => on [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 [HTTP_COOKIE] => PHPSESSID=752ee6d56e15f305233e30045987e5ce568c034; __qca=1176541225-59967328-5223185; [HTTP_HOST] => www.askapache.com [HTTP_REFERER] => http://www.askapache.com/protest/index.php?askapache=awesomeness&you=rock [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16 [IS_SUBREQ] => false [QUERY_STRING] => hi=you&whats=&you [REMOTE_ADDR] => 22.162.144.211 [REMOTE_HOST] => 22.162.144.211 [REMOTE_PORT] => 4605 [REMOTE_USER] => administrator [REQUEST_FILENAME] => /home/user/www_root/askapache.com/protest/index.php [REQUEST_METHOD] => GET [REQUEST_URI] => /protest/index.php [SCRIPT_FILENAME] => /home/user/www_root/askapache.com/protest/index.php [SCRIPT_GROUP] => daemonu [SCRIPT_USER] => askapache [SERVER_ADDR] => 208.113.134.190 [SERVER_ADMIN] => webmaster@askapache.com [SERVER_NAME] => www.askapache.com [SERVER_PORT] => 443 [SERVER_PROTOCOL] => HTTP/1.1 [SERVER_SOFTWARE] => Apache/2.0.61 (Unix) PHP/4.4.7 mod_ssl/2.0.61 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 [THE_REQUEST] => GET /protest/index.php?hi=you&whats=&you HTTP/1.1 [TIME] => 20080820015016 [TIME_DAY] => 20 [TIME_HOUR] => 01 [TIME_MIN] => 50 [TIME_MON] => 08 [TIME_SEC] => 16 [TIME_WDAY] => 3 [TIME_YEAR] => 2008Emulating ErrorDocuments with Mod_Rewrite
The ErrorDocument directive is helpful because an errordocument is called differently then a normal file, and it contains special variables to help an admin debug.
I’ve wanted to use a RewriteCond + a RewriteRule to cause an Apache ErrorDocument to be displayed for a long time… I finally figured it out. Simply use the HTTP STATUS CODE trick in combination with a simple RewriteRule to trigger an Apache ErrorDocument.
This code emulates the internal 404 process Apache goes through.. If the file is not found it requests the /test/trigger-error/404 internally which triggers the 404 ErrorDocument.
source: Crazy Advanced Mod_Rewrite
Introduction to .htaccess files
Your Web directory and its subdirectories can contain per-directory configuration files called .htaccess files. Whenever Stronghold (our Web server) receives a request for a file, it first looks for a file called .htaccess in that directory and its parent directories. If one is present, Stronghold considers the configuration directives within it before responding to the request. A .htaccess file works like this:
Some valid .htaccess configuration directives are listed in the next sections. There are several types of configuration directives that control different server features.
FileTypes
The server’s general configuration already includes a wide range of mappings from MIME types to filename suffixes. If you are using a file format, encoding type, or language that is not already included in the server’s configuration, you can use these directives to enable them for your files.
AddType
* Syntax: AddType MIME-type file-suffix
With AddType, you can map MIME types to filename suffixes without editing the mime.types file.
AddEncoding
* Syntax: AddEncoding encoding-type file-suffix
AddEncoding matches filename suffixes to encoding types. When Stronghold sends an encoded file to a client, it includes a Content-Encoding header that gives the encoding type based on the filename’s suffix. The client can then determine the type of pre-processing required to decode the file for the user.
AddLanguage
* Syntax: AddLanguage language-type filename-suffix
When a client requests a document, it should include the Accept-Language: field in its request. The value is a two-letter abbreviation such as en, it, fr, or jp. AddLanguage maps these language types to filename suffixes. For example, if you have files named readme.html.fr and readme.html.jp, Stronghold knows which one to send to a French client and which to send to a Japanese client when each sends a request for readme.htm
Handlers
Handlers perform preprocessing on requested files. You can also use the ErrorDocument directive to customize the error messages that Stronghold sends when a request fails.
ErrorDocument
* Syntax: ErrorDocument error-code document
In the event of a problem or error, Stronghold does one of four things,
All except the first are configured using ErrorDocument, which is followed by the HTTP response code and a message or URL. Messages in this context begin with a double quote (“), which does not form part of the message itself. Stronghold sometimes offers additional information regarding the problem or error. This can be embedded into the message using percentage signs (%).
URLs begin with a slash (/) for local URLs, or a full URL which the client can resolve. Examples:
AddHandler
* Syntax: AddHandler handler-name filename-suffix
This directive matches handlers to filename suffixes, and is often used in conjunction with Action. Handler-name can be the name of an existing handler or one that you create using the Stronghold API.
For example, you can use Stronghold’s as-is module by uncommenting this line in httpd.conf:
With this handle enabled, Stronghold sends any file ending in .asis without adding an HTTP header. Your .asis files must include their own HTTP headers, followed by two carriage returns. This means that you can attach custom headers to your files without creating special CGI scripts to manage them.
You can also use this directive to create special handlers specifically for the Action directive, as described below.
SetHandler
* Syntax: SetHandler handler
SetHandler specifies a handler to be used for all files in a directory or location. There are six built-in values for handler:
Handler Description
cgi-script All files are treated as CGI scripts and processed by mod_cgi.
imap-file All files are treated as imagemap files and processed by mod_imap.
send-as-is Stronghold send all files without appending HTTP headers.
server-info All files are sent with server configuration information.
server-status All files are sent with server status information.
server-parsed All files are treated as server-parsed HTML, for server-side includes by mod_ssi.
type-map All files are treated as type maps for content negotiation by mod_negotiation.
Handler can also be a third-party or custom handler that you add with AddHandler.
Action
* Syntax: Action handler|media-type script
Action maps handlers or media types to the CGI scripts that process them. For example, if you do not want to use Stronghold’s internal imagemap feature, you could include
This causes Stronghold to send all files ending in .map directly to the imap.cgi script. Thus, if Stronghold receives a request for
URL Rewriting
Stronghold can rewrite requested URLs that match a set of conditions. The rewritten request can be a URI, a URL, or a filepath with or without QUERY_STRING information. This powerful module can be used for an enormous variety of purposes, limited only by your imagination.
RewriteEngine
* Syntax: RewriteEngine on|off
This enables or disables the rewrite module for an object, host, or for the server. If you configure mod_rewrite for an object and later change your mind, set this directive to “off” rather than commenting out the rewrite directives.
RewriteOptions
* Syntax: RewriteOptions inherit
This sets special options for the rewrite configuration. Currently, there is only one option. “Inherit” forces an object to inherit the rewrite configuration of its parent. In the context of a .htaccess file, this means that the directory inherits the configuration in its parent’s .htaccess file.
RewriteBase
* Syntax: RewriteBase URL
This sets a base URL that mod_rewrite prepends to the result of a rewrite operation.
RewriteCond
* Syntax: RewriteCond test-string condition-pattern
RewriteCond defines a rule condition, and always precedes a RewriteRule directive. The rule given in RewriteRule applies only if the conditions given in RewriteCond are met. The rewrite module evaluates the test-string and matches it against condition-pattern. If they match, it applies the rule given in RewriteRule.
Test-string contains one or more of the following server variables:
Condition-pattern is a standard, extended regular expression that mod_rewrite applies to an instance of the test-string. It can be preceded by an exclamation mark (!) to indicate a non-matching pattern, or substituted with one of the following flags:
Flag Description
-d Indicates that the test-string is a path to a directory
-f Indicates that the test-string is a path to a file
-s Indicates that the test-string is a path to a file with a size greater than zero
-l Indicates that the test-string is a path to a symbolic link
-F Indicates that the test-string should be a path to a valid file that is accessible via the current access controls for the path, and instructs mod_rewrite to use an internal subrequest to check this. This may decrease Stronghold’s performance.
-U Indicates that the test-string should be a valid URL that is accessible via the current access controls for its path, and instructs mod_rewrite to use an internal subrequest to check this. This may decrease Stronghold’s performance.
Any of these can also be prepended by an exclamation mark (!).
Condition-pattern can also be appended by a comma-separated, bracketed list of one of the following rule flags:
Rule Flag Description
nocase Makes test-string and condition-pattern case-insensitive
N Same as “nocase”
ornext Used with multiple RewriteCond directives to combine them with OR instead of the implicit AND.
OR Same as “ornext”
For example:
RewriteRule
* Syntax: RewriteRule pattern substitute
RewriteRule defines the actual rules used to rewrite requested URLs that match conditions defined by RewriteCond. It can be used more than once, and multiple instances are evaluated in the order in which they appear.
Pattern is a POSIX regular expression that mod_rewrite applies to the current URL, whether it is the originally requested URL or a URL that has already been transformed by previous instances of RewriteRule. It may be prepended by an exclamation mark (!) to indicate a non-matching pattern, but a non-matching pattern cannot contain grouped wildcards.
Substitute is the string which mod_rewrite substitutes for any URL that matches the pattern. It can consist of
You can set special options for substitute by appending a comma-separated, bracketed list of these flags:
Flag Description
redirect[=status-code] Sends a status code between 300 and 400, specified by status-code. The default is 302.
R[=status-code] Same as “redirect”
forbidden Forces Stronghold to return a status code of 403 (“Forbidden”). This is useful for conditionally blocking URLs.
F Same as “forbidden”
gone Forces Stronghold to return a status code of 410 (“Gone”).
G Same as “gone”
proxy Forces the substitution part through the proxy module. The substitution string must be a valid URI. This is useful as a sophisticated alternative to the ProxyPass directive.
P Same as “proxy”
last Force the rewrite process to end here
L Same as “last”
next Reruns the rewrite process, starting with the first instance of RewriteRule and using the outcome of the current rewrite process as new input
N Same as “next”
chain Chains the current rule with the next rule, provided that the current rule matches
C Same as “chain”
type=mime-type Forces Stronghold to return the file as the specified MIME type
T=mime-type Same as “type”
nosubreq Indicates that the current rule applies only if the current request is not an internal subrequest
NS Same as “nosubreq”
passthrough Passes the substitution to the next handler, which should immediately follow the current RewriteRule
PT Same as “passthrough”
skip=n Skips the next n rules in a sequence if the current rule matches
S=n Same as “skip”
env=VARIABLE:VALUE Sets the environment variable VARIABLE to the value VALUE
E=VARIABLE:VALUE Same as “env”
User authentication
Stronghold includes authentication options:
AuthGroupFile
* Syntax: AuthGroupFile filename
AuthGroupFile specifies the filename that contains the list of authentication groups and the users they include.
AuthUserFile
* Syntax: AuthUserFile filename
AuthUserFile specifies the filename that contains the list of authenticated users and their crypt()-encrypted passwords.
Require
* Syntax: Require entity-name entity1 [entity2 ...]
This directive selects which authenticated users can access a directory. The allowed values are
Value Description
require user userid userid …
require group group-name group-name … Only users in the named groups can access the directory.
require valid-user All valid users can access the directory.
If require appears in a <Limit> section, then it restricts access to the named methods, otherwise it restricts access for all methods. For example:
Require must be accompanied by AuthName and AuthType directives, and directives such as AuthUserFile and AuthGroupFile (to define users and groups) in order to work correctly.
Host-based access control
In many cases, you may want to allow only certain hosts to access your hosts, directories, or files. Host-based access control is not as reliable as certificate authentication, because hackers can “spoof” hosts, pretending to send requests from one of the hosts you allow. However, we do recommend that you use these directives to control access to server status and configuration information, like this:
<Location /stronghold-info>
order deny, allow
deny from all
allow from yourhost.com
</Location>
With this configuration, only users on your host can access server information.
order
* Syntax: order allow,deny|deny,allow|mutual-failure
Order, used in conjunction with allow and deny, provides host-based access control. The value is a comma-separated list that indicates which directive takes priority. The one that does not have priority usually takes the value “all.” For example,
allows access from a few hosts and deny access from all other hosts. If you enter
then you can deny access from a few hosts and allow access from all others. The order you choose depends on the nature of your site.
If the value is “mutual-failure,” then access is limited to hosts that appear in the allow list and not in the deny list.
Satisfy
* Syntax: Satisfy any|all
This sets the access policy if both allow and require are used. The value can be either “all” or “any.”
allow
* Syntax: allow from all|hostname1 [hostname2 ...]
The value for the allow directive can either be a list of hostnames which are allowed to access your server, or “all” to allow access from all hosts not excluded by the deny directive.
deny
* Syntax: deny from all|hostname1 [hostname2 ...]
The value for the deny directive can either be a list of hostnames which are denied access to your server, or “all” to deny access from all hosts not included by the allow directive.
Directory indexing
When Stronghold receives a request for a directory that does not have a default file, it creates a directory index in HTML and sends it to the client. These directives control the parameters of the generated index.
DirectoryIndex
* Syntax: DirectoryIndex filename1 [filename2 ...] /path/to/error/page
This sets the filename of the default index file for any directory. When a client sends a request URL to a directory but not a specific file, Stronghold returns the DirectoryIndex file. You can list any number of filenames, in order of priority, and Stronghold will look for each and return the first one it finds. By specifying the path to a file that contains an error message as the last item on the list, you can cause Stronghold to return that message if it finds none of the previous filenames. Alternatively, you can simply allow Stronghold to automatically create an HTML-formatted file list for the requested directory if it finds none of the files in the list.
Text: . Any single character [chars] Character class: Any character of the class ``chars'' [^chars] Character class: Not a character of the class ``chars'' text1|text2 Alternative: text1 or text2 Quantifiers: ? 0 or 1 occurrences of the preceding text * 0 or N occurrences of the preceding text (N > 0) + 1 or N occurrences of the preceding text (N > 1) Grouping: (text) Grouping of text (used either to set the borders of an alternative as above, or to make backreferences, where the Nth group can be referred to on the RHS of a RewriteRule as $N) Anchors: ^ Start-of-line anchor $ End-of-line anchor Escaping: \char escape the given char (for instance, to specify the chars ".[]()" etc.)
Mod_Rewrite Page to Remote Server
Introduction
RewriteRules can have their behavior modified by one or more flags. Flags are included in square brackets at the end of the rule, and multiple flags are separated by commas.
RewriteRule pattern target [Flag1,Flag2,Flag3]
The flags all have a short form, such as CO, as well as a longer form, such as cookie. Some flags take one or more arguments. Flags are not case sensitive.
top
The flags
Each flag has a long and short form. While it is most common to use the short form, it is recommended that you familiarize yourself with the long form, so that you remember what each flag is supposed to do.
Presented here are each of the available flags, along with an example of how you might use them.
C|chain
The [C] or [chain] flag indicates that the RewriteRule is chained to the next rule. That is, if the rule matches, then it is processed as usual and control moves on to the next rule. However, if it does not match, then the next rule, and any other rules that are chained together, will be skipped.
CO|cookie
The [CO], or [cookie] flag, allows you to set a cookie when a particular RewriteRule matches. The argument consists of three required fields and two optional fields.
You must declare a name and value for the cookie to be set, and the domain for which you wish the cookie to be valid. You may optionally set the lifetime of the cookie, and the path for which it should be returned.
By default, the lifetime of the cookie is the current browser session.
By default, the path for which the cookie will be valid is “/” – that is, the entire website.
Several examples are offered here:
This rule doesn’t rewrite the request (the “-” rewrite target tells mod_rewrite to pass the request through unchanged) but sets a cookie called ‘frontdoor’ to a value of ‘yes’. The cookie is valid for any host in the .apache.org domain. It will be set to expire in 1440 minutes (24 hours) and will be returned for all URIs.
E|env
With the [E], or [env] flag, you can set the value of an environment variable. Note that some environment variables may be set after the rule is run, thus unsetting what you have set. See the Environment Variables document for more details on how Environment variables work.
The following example sets an evironment variable called ‘image’ to a value of ’1′ if the requested URI is an image file. Then, that environment variable is used to exclude those requests from the access log.
Note that this same effect can be obtained using SetEnvIf. This technique is offered as an example, not as a recommendation.
F|forbidden
Using the [F] flag causes Apache to return a 403 Forbidden status code to the client. While the same behavior can be accomplished using the Deny directive, this allows more flexibility in assigning a Forbidden status.
The following rule will forbid .exe files from being downloaded from your server.
This rule uses the “-” syntax for the rewrite target, which means that the requested URI is not modified.
G|gone
Gone flag
H|handler
Handler flag
L|last
Last flag
N|next
Next round flag
NC|nocase
Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn’t care whether letters appear as upper-case or lower-case in the matched URI.
In the example below, any request for an image file will be proxied to your dedicated image server. The match is case-insensitive, so that .jpg and .JPG files are both acceptable, for example.
NE|noescape
No escape flag
NS|nosubreq
No internal subrequest flag
P|proxy
Proxy flag
PT|passthrough
Passthrough flag
QSA|qsappend
Query String Append flag
R|redirect
Redirect flag
S|skip
The [S] flag is used to skip rules that you don’t want to run. This can be thought of as a goto statement in your rewrite ruleset. In the following example, we only want to run the RewriteRule if the requested URI doesn’t correspond with an actual file.
RewriteRule (.*\.gif) images.php?$1
RewriteRule (.*\.html) docs.php?$1
This technique is useful because a RewriteCond only applies to the RewriteRule immediately following it. Thus, if you want to make a RewriteCond apply to several RewriteRules, one possible technique is to negate those conditions and use a [Skip] flag.
T|type
Type flag
htaccess from the Apache htaccess tutorial. Check out and use the 404 Error Page WordPress Plugin
Options: ALL,FollowSymLinks,Includes,IncludesNOEXEC,SymLinksIfOwnerMatch ########## ## MAIN DEFAULTS ### Options +ExecCGI -Indexes DirectoryIndex index.html index.htm index.php DefaultLanguage en-US AddDefaultCharset UTF-8 ServerSignature Off ## ENVIRONMENT VARIABLES ### SetEnv PHPRC /webroot/includes SetEnv TZ America/Indianapolis SetEnv SERVER_ADMIN webmaster@domain.tld ## MIME TYPES ### AddType video/x-flv .flv AddType application/x-shockwave-flash .swf AddType image/x-icon .ico ## FORCE FILE TO DOWNLOAD INSTEAD OF APPEAR IN BROWSER ### -> http://www.htaccesselite.com/addtype-addhandler-action-vf6.html AddType application/octet-stream .mov .mp3 .zip
======== 1xx ErrorDocument 100 /error-100/ ErrorDocument 101 /error-101/ ErrorDocument 102 /error-102/ ======== 2xx ErrorDocument 200 /error-200/ ErrorDocument 201 /error-201/ ErrorDocument 202 /error-202/ ErrorDocument 203 /error-203/ ErrorDocument 204 /error-204/ ErrorDocument 205 /error-205/ ErrorDocument 206 /error-206/ ErrorDocument 207 /error-207/ ======== 4xx ErrorDocument 400 /error-400/ ErrorDocument 401 /error-401/ ErrorDocument 402 /error-402/ ErrorDocument 403 /error-403/ ErrorDocument 404 /error-404/ ErrorDocument 405 /error-405/ ErrorDocument 406 /error-406/ ErrorDocument 407 /error-407/ ErrorDocument 408 /error-408/ ErrorDocument 409 /error-409/ ErrorDocument 410 /error-410/ ErrorDocument 411 /error-411/ ErrorDocument 412 /error-412/ ErrorDocument 413 /error-413/ ErrorDocument 414 /error-414/ ErrorDocument 415 /error-415/ ErrorDocument 416 /error-416/ ErrorDocument 417 /error-417/ ErrorDocument 418 /error-418/ ErrorDocument 419 /error-419/ ErrorDocument 420 /error-420/ ErrorDocument 421 /error-421/ ErrorDocument 422 /error-422/ ErrorDocument 423 /error-423/ ErrorDocument 424 /error-424/ ErrorDocument 425 /error-425/ ErrorDocument 426 /error-426/ ======== 5xx ErrorDocument 500 /error-500/ ErrorDocument 501 /error-501/ ErrorDocument 502 /error-502/ ErrorDocument 503 /error-503/ ErrorDocument 504 /error-504/ ErrorDocument 505 /error-505/ ErrorDocument 506 /error-506/ ErrorDocument 507 /error-507/ ErrorDocument 508 /error-508/ ErrorDocument 509 /error-509/ ErrorDocument 510 /error-510/
AddLanguage aa .aa # Afar AddLanguage ab .ab # Abkhazian AddLanguage af .af # Afrikaans AddLanguage am .am # Amharic AddLanguage gu .gu # Gujarati AddLanguage ha .ha # Hausa AddLanguage he .he # Hebrew AddLanguage hi .hi # Hindi AddLanguage hr .hr # Croatian AddLanguage hu .hu # Hungarian AddLanguage hy .hy # Armenian AddLanguage ia .ia # Interlingua AddLanguage id .id # Indonesian AddLanguage ie .ie # lnteriingue AddLanguage ik .ik # Knupiak AddLanguage is .is # Icelandic AddLanguage it .it # Italian AddLanguage iu .iu # Inuktitut (Eskimo) AddLanguage ja .ja # Japanese AddLanguage jw .jw # Javanese AddLanguage ka .ka # Georgian AddLanguage kk .kk # Kazakh AddLanguage kl .kl # Greaenlandic AddLanguage km .km # Cambodian AddLanguage kn .kn # Kannada AddLanguage ko .ko # Korean AddLanguage ks .ks # Kashmiri AddLanguage ku .ku # Kurdish AddLanguage ky .ky # Kirghiz AddLanguage la .la # Latin AddLanguage ln .ln # Lingala AddLanguage lo .lo # Laothian AddLanguage lt .lt # Lithuainnian AddLanguage lv .lv # Latvian, Lettish AddLanguage mg .mg # Malagasy AddLanguage mi .mi # Maori AddLanguage uk .uk # Ukrainian AddLanguage ur .ur # Urdu AddLanguage uz .uz # Uzbek AddLanguage vi .vi # Vietnamese AddLanguage vo .vo # Volapuek AddLanguage wo .wo # Wolof AddLanguage xh .xh # Xhosa AddLanguage yi .yi # Yiddish AddLanguage yo .yo # Yoruba AddLanguage za .za # Zhuang AddLanguage zh .zh # Chinese AddLanguage zu .zu # Zulu
Handlers be builtin, included in a module, or added with Action directive
default-handler: default, handles static content (core)
send-as-is: Send file with HTTP headers (mod_asis)
cgi-script: treat file as CGI script (mod_cgi)
imap-file: Parse as an imagemap rule file (mod_imap)
server-info: Get server config info (mod_info)
server-status: Get server status report (mod_status)
type-map: type map file for content negotiation (mod_negotiation)
fastcgi-script: treat file as fastcgi script (mod_fastcgi)
##########
-> http://www.askapache.com/php/
## PARSE AS CGI ###
AddHandler cgi-script .cgi .pl .spl
## RUN PHP AS APACHE MODULE ###
AddHandler application/x-httpd-php .php .htm
## RUN PHP AS CGI ###
AddHandler php-cgi .php .htm
## CGI PHP WRAPPER FOR CUSTOM PHP.INI ###
AddHandler phpini-cgi .php .htm
Action phpini-cgi /cgi-bin/php5-custom-ini.cgi
## FAST-CGI SETUP WITH PHP-CGI WRAPPER FOR CUSTOM PHP.INI ###
AddHandler fastcgi-script .fcgi
AddHandler php-cgi .php .htm
Action php-cgi /cgi-bin/php5-wrapper.fcgi
## CUSTOM PHP CGI BINARY SETUP ###
AddHandler php-cgi .php .htm
Action php-cgi /cgi-bin/php.cgi
## PROCESS SPECIFIC FILETYPES WITH CGI-SCRIPT ###
Action image/gif /cgi-bin/img-create.cgi
## CREATE CUSTOM HANDLER FOR SPECIFIC FILE EXTENSIONS ###
AddHandler custom-processor .ssp
Action custom-processor /cgi-bin/myprocessor.cgi
300 5 M
2700 45 M
3600 1 H
54000 15 H
86400 1 D
518400 6 D
604800 1 W
1814400 3 W
2419200 1 M
26611200 11 M
29030400 1 Y (never expire)
-> http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html Header set Cache-Control "max-age=2592000" Header set Cache-Control "max-age=604800" Header set Cache-Control "max-age=600" Header unset Cache-Control ## ALTERNATE EXPIRES CACHING ### -> htaccesselite.com/d/use-htaccess-to-speed-up-your-site-discussion-vt67.html ExpiresActive On ExpiresDefault A604800 ExpiresByType image/x-icon A2592000 ExpiresByType application/x-javascript A2592000 ExpiresByType text/css A2592000 ExpiresByType text/html A300 ExpiresActive Off ## META HTTP-EQUIV REPLACEMENTS ### Header set imagetoolbar "no"
REQUEST METHODS: GET,POST,PUT,DELETE,CONNECT,OPTIONS,PATCH,PROPFIND,
PROPPATCH,MKCOL,COPY,MOVE,LOCK,UNLOCK
##########
## REWRITE DEFAULTS ###
RewriteEngine On
RewriteBase /
## REQUIRE SUBDOMAIN ###
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^subdomain\.domain\.tld$ [NC]
RewriteRule ^/(.*)$ http://subdomain.domain.tld/$1 [L,R=301]
## SEO REWRITES ###
RewriteRule ^(.*)/ve/(.*)$ $1/voluntary-employee/$2 [L,R=301]
RewriteRule ^(.*)/hsa/(.*)$ $1/health-saving-account/$2 [L,R=301]
## WORDPRESS ###
RewriteCond %{REQUEST_FILENAME} !-f # Existing File
RewriteCond %{REQUEST_FILENAME} !-d # Existing Directory
RewriteRule . /index.php [L]
## ALTERNATIVE ANTI-HOTLINKING ###
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(subdomain\.)?domain.tld/.*$ [NC]
RewriteRule ^.*\.(bmp|tif|gif|jpg|jpeg|jpe|png)$ - [F]
## REDIRECT HOTLINKERS ###
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(subdomain\.)?domain.tld/.*$ [NC]
RewriteRule ^.*\.(bmp|tif|gif|jpg|jpeg|jpe|png)$ http://google.com [R]
## DENY REQUEST BASED ON REQUEST METHOD ###
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD)$ [NC]
RewriteRule ^.*$ - [F]
## REDIRECT UPLOADS ###
RewriteCond %{REQUEST_METHOD} ^(PUT|POST)$ [NC]
RewriteRule ^(.*)$ /cgi-bin/form-upload-processor.cgi?p=$1 [L,QSA]
## REQUIRE SSL EVEN WHEN MOD_SSL IS NOT LOADED ###
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
### ALTERNATATIVE TO USING ERRORDOCUMENT ###
-> http://www.htaccesselite.com/d/htaccess-errordocument-examples-vt11.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /error.php [L]
## SEO REDIRECTS ###
Redirect 301 /2006/oldfile.html http://subdomain.domain.tld/newfile.html
RedirectMatch 301 /o/(.*)$ http://subdomain.domain.tld/s/dl/$1
Require (user|group|valid-user) (username|groupname)
##########
## BASIC PASSWORD PROTECTION ###
AuthType basic
AuthName "prompt"
AuthUserFile /.htpasswd
AuthGroupFile /dev/null
Require valid-user
## ALLOW FROM IP OR VALID PASSWORD ###
Require valid-user
Allow from 192.168.1.23
Satisfy Any
## PROTECT FILES ###
Order Allow,Deny
Deny from all
## PREVENT HOTLINKING ###
SetEnvIfNoCase Referer "^http://subdomain.domain.tld/" good
SetEnvIfNoCase Referer "^$" good
Order Deny,Allow
Deny from all
Allow from env=good
ErrorDocument 403 http://www.google.com/intl/en_ALL/images/logo.gif
ErrorDocument 403 /images/you_bad_hotlinker.gif
## LIMIT UPLOAD FILE SIZE TO PROTECT AGAINST DOS ATTACK ###
LimitRequestBody 10240000 #bytes, 0-2147483647(2GB)
=============================================================================#
SSL SECURITY
=============================================================================#
-> http://www.askapache.com/htaccess/ssl-example-usage-in-htaccess.html
##########
## MOST SECURE WAY TO REQUIRE SSL ###
-> http://www.askapache.com/htaccess/apache-ssl-in-htaccess-examples.html
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq "domain.tld"
ErrorDocument 403 https://domain.tld
SITE UNDER CONSTRUCTION
=============================================================================#
Heres some awesome htaccess to use when you are developing a site
##########
## COMBINED DEVELOPER HTACCESS CODE-USE THIS ###
Header set Cache-Control "max-age=5"
AuthType basic
AuthName "Ooops! Temporarily Under Construction..."
AuthUserFile /.htpasswd
AuthGroupFile /dev/null
Require valid-user # password prompt for everyone else
Order Deny,Allow
Deny from all
Allow from 192.168.64.5 # Your, the developers IP address
Allow from w3.org # css/xhtml check jigsaw.w3.org/css-validator/
Allow from googlebot.com # Allows google to crawl your pages
Satisfy Any # no password required if host/ip is Allowed
## DONT HAVE TO EMPTY CACHE OR RELOAD TO SEE CHANGES ###
ExpiresDefault A5 #If using mod_expires
Header set Cache-Control "max-age=5"
## ALLOW ACCESS WITH PASSWORD OR NO PASSWORD FOR SPECIFIC IP/HOSTS ###
AuthType basic
AuthName "Ooops! Temporarily Under Construction..."
AuthUserFile /.htpasswd
AuthGroupFile /dev/null
Require valid-user # password prompt for everyone else
Order Deny,Allow
Deny from all
Allow from 192.168.64.5 # Your, the developers IP address
Allow from w3.org # css/xhtml check jigsaw.w3.org/css-validator/
Allow from googlebot.com # Allows google to crawl your pages
Satisfy Any # no password required if host/ip is Allowed
htaccess and .htaccess rewrite links
Apache Documentation
First, here are several links to the definitive source for Apache 1.3 and Apache 2.0 specifically related to using .htaccess, especially for redirecting URLs and blocking bad bots and spammers.
Apache 1.3
By Ralf S. Engelschall
Apache 2.0
By Ralf S. Engelschall
How to Use .htaccess, mod_rewrite, and Related (for Apache)
Introduction to .htaccess, including what you can do with .htaccess, creating custom error pages, deny/allow access to specific pages or directories, password protection, redirecting URLs, and more. By David Gowans, via freewebmasterhelp.com.
By Andy King, via websiteoptimization.com.
For mod_rewrite beginners, by DaveAtIFG via Webmasterworld, Dec 16, 2002.
By Tamas Turcsanyi, via SitePoint, October 22, 2002.
By Daniel via 4webhelp.net, updated February 09, 2004.
by Bill Humphries via A List Apart, June 30, 2000.
Helpful, easy-to-understand introduction to .htaccess and what you can do with it. Collated by Miraz Jordan via wise-women.org.
SRC: http://brainstormsandraves.com/archives/2005/10/09/htaccess/