Friday, October 13, 2006

Frequently Used .htaccess Directives in Apache

Frequently Used .htaccess Directives in Apache

# Force www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain\.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
or
RewriteRule ^(.*)$ http://www.domain.com/$0 [R=301,L]

# Add mod-rewrite rules (if needed)
# This checks if a file or directory exists before calling the var
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# This sends the var to the PHP file
RewriteRule ^([A-Za-z0-9_-]+) http://www.domain.com/foo/bar/get.php?var=$0 [PT]

# Disable and turn off PHP register globals
php_flag register_globals OFF

# Enable PHP errors
php_flag display_errors ON

# Disable directory listing
IndexIgnore *


# Override common PHP settings
php_value post_max_size 16M
php_value upload_max_filesize 20M
php_value memory_limit 25M
php_value max_execution_time 900
php_value session.gc_maxlifetime 7200


# Hide the directory indexes
Options All -Indexes

# Show the directory indexes
Options All +Indexes


# Disable access and prevent viewing of htaccess

opentag Files .htaccess closetag
order allow,deny
deny from all
opentag /Files closetag

Alternatively,
CHMOD .htaccess to 644 or RW-R--R--

# Disallow or prevent hotlinking of images, photos or any other file type
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]


# Redirect an old path to a new one
# Redirect an old file to a new file
Redirect /old-dir/foo.html http://www.domain.com/foo/new.html

# Redirect an old directory to a new directory
Redirect /old-dir/ http://www.domain.com/new-dir/

# Set the default index file
DirectoryIndex index.html
or

# Set multiple files as the default if the first doesn't exist
DirectoryIndex index1.html index2.php index3.shtml foo.htm

# Block or ban offline browsers or leechers
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^wget [OR]
RewriteCond %{HTTP_USER_AGENT} ^flashget [OR]
RewriteCond %{HTTP_USER_AGENT} ^getright
RewriteRule ^.* - [F,L]

# Ban traffic from a single or multiple domains
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} foo\.com [NC]
RewriteRule .* - [F]

or

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} foo1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} foo2\.com
RewriteRule .* - [F]

No comments: