htaccess Tricks for Webmasters

Apache For Webmasters is some of my favorite htaccess examples from some of my favorite .htaccess tutorials. These cut-and-paste ready htaccess code snippets are very useful for website and server administrators.

.htaccess Tutorial Index |

When site is "Under Construction"

This lets google crawl, lets me access ( without a password, and lets access from anyone WITH a password. It also allows for XHTML and CSS validation through

Article: Instruct Search Engines with a 503 Service Unavailabe header and Retry-After header while site is offline

update combined allow code credit: megaspaz

AuthName "Under Development"
AuthUserFile /web/
AuthType basic
Require valid-user
Order Deny,Allow
Deny from all
Allow from
Satisfy Any

Redirect everyone to different site except 1 IP

ErrorDocument 403
Order deny,allow
Deny from all
Allow from

Redirect all but 1 IP to different site, using mod_rewrite

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^
RewriteRule .* [R=302,L]

Redirect Everyone but you to alternate page on your server.

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^
RewriteCond %{REQUEST_URI} !/temporary-offline.html$
RewriteRule .* /temporary-offline.html [R=302,L]

Set the Timezone of the server

SetEnv TZ America/Las_Vegas

Set the Server Administrator Email

Include a link to your email address in Apache-Generated Error Documents. Shows up on default Apache error pages

ServerSignature EMail

Turn off the ServerSignature

Turns off the address part of your Server Signature in Apache generated Error Documents.

ServerSignature Off

Force File download, do not display/open in browser

In your HTML directly link to the file.. Download Movie1. Now you will get a pop-up box asking whether you want to save the file or open it.

AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

Process .gif files with a cgi script

When a user requests a .gif file (image/gif) the server instead of serving that .gif file it serves the results of /cgi-bin/filter.cgi

Action image/gif /cgi-bin/filter.cgi

Process Requests with certain Request Methods

Article: Request Methods

Script PUT /cgi-bin/upload.cgi
Script HEAD /cgi-bin/head-robot.cgi

Make any file be a certain filetype

Makes image.gif, blah.html, index.cgi all act as php

ForceType application/x-httpd-php

Use IfModule for robustness

Basically if the module is found it will perform the code inside the tag.

# any mod_rewrite directives here

# any Expires Directives go here

# any Header directives go here

htaccess Guide Sections

.htaccess Tutorial Index |