31 htaccess Scripts To Make Your Server More Powerful | Syber Blog

Posted by Syberplanet on May 23rd, 2009 – 14,637 views

htaccess snippets

Redirect Everyone Except IP address to alternate page

ErrorDocument 403 http://www.yahoo.com/
Order deny,allow

Deny from all
Allow from

When developing sites

AuthName “Under Development”
AuthUserFile /home/sitename.com/.htpasswd
AuthType basic
Require valid-user
Order deny,allow
Deny from all
Allow from w3.org htmlhelp.com googlebot.com
Satisfy Any

This lets google crawl the page, lets me access without a password, and lets my client access the page WITH a password. It also allows for XHTML and CSS validation! (w3.org)

Fix double-login prompt

SSLOptions +StrictRequire
SSLRequire %{HTTP_HOST} eq “askapache.com”

ErrorDocument 403 https://askapache.com

Redirect non-https requests to https server and ensure that .htpasswd authorization can only be entered across HTTPS

Set Timezone of the Server (GMT)

SetEnv TZ America/Indianapolis

Administrator Email for ErrorDocument

SetEnv SERVER_ADMIN webmaster@google.com
ServerSignature for ErrorDocument
ServerSignature off | on | email

Charset and Language headers

AddDefaultCharset UTF-8
DefaultLanguage en-US

Disallow Script Execution

Options -ExecCGI
AddHandler cgi-scriptscript .php .pl .py .jsp .asp .htm .shtml .sh .cgi

Deny Request Methods


RewriteRule .* – [F]

Force “File Save As” Prompt

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

Show CGI Source Code

RemoveHandler cgi-scriptscript .pl .py .cgi
AddType text/plain .pl .py .cgi

Serve all .pdf files on your site using .htaccess and mod_rewrite

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f

RewriteRule ^(.+).pdf$ /cgi-bin/pdf.php?file=$1 [L,NC,QSA]

Rewrite to www

RewriteCond %{REQUEST_URI} !^/(robots.txt|favicon.ico|sitemap.xml)$
RewriteCond %{HTTP_HOST} !^www.askapache.com$ [NC]

RewriteRule ^(.*)$ http://www.askapache.com/$1 [R=301,L]

Rewrite to www dynamically

RewriteCond %{REQUEST_URI} !^/robots.txt$ [NC]
RewriteCond %{HTTP_HOST} !^www.[a-z-]+.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]

301 Redirect Old File

Redirect 301 /old/file.html http://www.askapache.com/new/file.html

301 Redirect Entire Directory

RedirectMatch 301 /blog(.*) http://www.askapache.com/$1

Protecting your php.cgi

Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS

Set Cookie based on Request

2nd parantheses.
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ – [co=lang:$2:.askapache.com:7200:/]

This code sends the Set-Cookie header to create a cookie on the client with the value of a matching item in

Set Cookie with env variable

Header set Set-Cookie “language=%{lang}e; path=/;” env=lang

Custom ErrorDocuments

ErrorDocument 100 /error/100_CONTINUE.html
ErrorDocument 101 /error/101_SWITCHING_PROTOCOLS.html
ErrorDocument 102 /error/102_PROCESSING.html
ErrorDocument 200 /error/200_OK.html
ErrorDocument 201 /error/201_CREATED.html
ErrorDocument 202 /error/202_ACCEPTED.html
ErrorDocument 203 /error/203_NON_AUTHORITATIVE.html
ErrorDocument 204 /error/204_NO_CONTENT.html
ErrorDocument 205 /error/205_RESET_CONTENT.html
ErrorDocument 206 /error/206_PARTIAL_CONTENT.html
ErrorDocument 207 /error/207_MULTI_STATUS.html
ErrorDocument 300 /error/300_MULTIPLE_CHOICES.html
ErrorDocument 301 /error/301_MOVED_PERMANENTLY.html
ErrorDocument 302 /error/302_MOVED_TEMPORARILY.html
ErrorDocument 303 /error/303_SEE_OTHER.html
ErrorDocument 304 /error/304_NOT_MODIFIED.html
ErrorDocument 305 /error/305_USE_PROXY.html
ErrorDocument 307 /error/307_TEMPORARY_REDIRECT.html
ErrorDocument 400 /error/400_BAD_REQUEST.html
ErrorDocument 401 /error/401_UNAUTHORIZED.html
ErrorDocument 402 /error/402_PAYMENT_REQUIRED.html
ErrorDocument 403 /error/403_FORBIDDEN.html
ErrorDocument 404 /error/404_NOT_FOUND.html
ErrorDocument 405 /error/405_METHOD_NOT_ALLOWED.html
ErrorDocument 406 /error/406_NOT_ACCEPTABLE.html
ErrorDocument 407 /error/407_PROXY_AUTHENTICATION_REQUIRED.html
ErrorDocument 408 /error/408_REQUEST_TIME_OUT.html
ErrorDocument 409 /error/409_CONFLICT.html
ErrorDocument 410 /error/410_GONE.html
ErrorDocument 411 /error/411_LENGTH_REQUIRED.html
ErrorDocument 412 /error/412_PRECONDITION_FAILED.html
ErrorDocument 413 /error/413_REQUEST_ENTITY_TOO_LARGE.html
ErrorDocument 414 /error/414_REQUEST_URI_TOO_LARGE.html
ErrorDocument 415 /error/415_UNSUPPORTED_MEDIA_TYPE.html
ErrorDocument 416 /error/416_RANGE_NOT_SATISFIABLE.html
ErrorDocument 417 /error/417_EXPECTATION_FAILED.html
ErrorDocument 422 /error/422_UNPROCESSABLE_ENTITY.html
ErrorDocument 423 /error/423_LOCKED.html
ErrorDocument 424 /error/424_FAILED_DEPENDENCY.html
ErrorDocument 426 /error/426_UPGRADE_REQUIRED.html
ErrorDocument 500 /error/500_INTERNAL_SERVER_ERROR.html
ErrorDocument 501 /error/501_NOT_IMPLEMENTED.html
ErrorDocument 502 /error/502_BAD_GATEWAY.html
ErrorDocument 503 /error/503_SERVICE_UNAVAILABLE.html
ErrorDocument 504 /error/504_GATEWAY_TIME_OUT.html
ErrorDocument 505 /error/505_VERSION_NOT_SUPPORTED.html
ErrorDocument 506 /error/506_VARIANT_ALSO_VARIES.html
ErrorDocument 507 /error/507_INSUFFICIENT_STORAGE.html
ErrorDocument 510 /error/510_NOT_EXTENDED.html

Implementing a Caching Scheme with .htaccess

#1 year
Header set Cache-Control “public”
Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”
Header unset Last-Modified

#2 hours
Header set Cache-Control “max-age=7200, must-revalidate”
SetOutputFilter DEFLATE
Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”

Password Protect single file

AuthName “Prompt”
AuthType Basic
AuthUserFile /home/askapache.com/.htpasswd

Require valid-user

Password Protect multiple files

AuthName “Development”
AuthUserFile /.htpasswd
AuthType basic
Require valid-user

Send Custom Headers

Header set P3P “policyref=”http://www.askapache.com/w3c/p3p.xml“”
Header set X-Pingback “http://www.askapache.com/xmlrpc.php”
Header set Content-Language “en-US”
Header set Vary “Accept-Encoding”

Blocking based on User-Agent Header

SetEnvIfNoCase ^UserUser-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT

SetEnvIfNoCase ^UserUser-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
Deny from env=HTTP_SAFE_BADBOT

Blocking with RewriteCond

RewriteCond %{HTTP_USER_AGENT} ^.*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures).*$ [NC]
RewriteRule . – [F,L]

.htaccess for mod_php

SetEnv PHPRC /location/todir/containing/phpinifile

.htaccess for php as cgi

AddHandler php-cgi .php .htm
Action php-cgi /cgi-bin/php5.cgi

Add values from HTTP Headers

SetEnvIfNoCase ^If-Modified-Since$ “(.+)” HTTP_IF_MODIFIED_SINCE=$1
SetEnvIfNoCase ^If-None-Match$ “(.+)” HTTP_IF_NONE_MATCH=$1
SetEnvIfNoCase ^Cache-Control$ “(.+)” HTTP_CACHE_CONTROL=$1
SetEnvIfNoCase ^Connection$ “(.+)” HTTP_CONNECTION=$1
SetEnvIfNoCase ^Keep-Alive$ “(.+)” HTTP_KEEP_ALIVE=$1
SetEnvIfNoCase ^Authorization$ “(.+)” HTTP_AUTHORIZATION=$1
SetEnvIfNoCase ^Cookie$ “(.+)” HTTP_MY_COOKIE=$1

Stop hotlinking

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?askapache.com/.*$ [NC]

RewriteRule .(gif|jpg|swf|flv|png)$ http://www.askapache.com/feed.gif [R=302,L]

Turn logging off for IP

SecFilterSelective REMOTE_ADDR “″ “nolog,noauditlog,pass”

Turn logging on for IP

SecFilterSelective REMOTE_ADDR “!^″ “nolog,noauditlog,pass”
SecFilterSelective REMOTE_ADDR “″ “log,auditlog,pass”

Stumble Upon Reddit Design Float Design Float Technorati