.htaccess examples

(O_o)

H̨̼̩͐̑͆̀̚&
Sep 23, 2010
4,719
91
0
L̇ͥͧ̑͋ͥ̏̔͆́̋̂̆̌̚̚&#8
just sharing this bc I thought some could find it useful. Feel free to add any you think others could use!

Rewrite a domain alias to subfolder of target site:
Code:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/subdomain/.*
RewriteRule ^(.*) /subdomain/$1 [L]


Disable a testing URL/Alias by rewriting to target domain:
Code:
RewriteCond %{HTTP_HOST} ^[www\.]*testlink.websitesettings.com [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,NC]

Block single IP address:
Code:
SetEnvIf X-Cluster-Client-Ip ^123.45.67.89 DenyAccess 
Order Allow,Deny 
Deny from env=DenyAccess 
Allow from all

Allow from single IP address:
Code:
SetEnvIf X-Cluster-Client-Ip ^123.45.67.89 allowclient
order deny,allow
deny from all
allow from env=allowclient

Set timezone:
Code:
#Replace LOCATION with valid timezone from the reference below
#Time Zone List: http://www.php.net/manual/en/timezones.php
#SET time_zone = timezone (mysql)
SetEnv TZ LOCATION

Set character encoding:
Code:
AddDefaultCharset iso-8859-1
 


Here is parts of mine.

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##


# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP#

# Block SEMalt botnet
SetEnvIfNoCase Referer fbdownloader.com spammer=yes
SetEnvIfNoCase Referer descargar-musicas-gratis.com spammer=yes
SetEnvIfNoCase Referer baixar-musicas-gratis.com spammer=yes
SetEnvIfNoCase Referer savetubevideo.com spammer=yes
SetEnvIfNoCase Referer srecorder.com spammer=yes
SetEnvIfNoCase Referer kambasoft.com spammer=yes
SetEnvIfNoCase Referer semalt.com spammer=yes
SetEnvIfNoCase Via evil-spam-proxy spammer=yes
SetEnvIfNoCase Referer evil-spam-domain.com spammer=yes
SetEnvIfNoCase Referer evil-spam-keyword spammer=yes
SetEnvIfNoCase Via pinappleproxy spammer=yes
SetEnvIfNoCase Referer poker spammer=yes

Order allow,deny
Allow from all
Deny from env=spammer

# Block fake traffic
RewriteEngine on
Options +FollowSymlinks
# Block all http and https referrals from “savetubevideo.com” and all subdomains of “savetubevideo.com”
RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)*savetubevideo\.com\ [NC,OR]
# Block all http and https referrals from “srecorder.com” and all subdomains of “srecorder.com”
RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)*srecorder\.com\ [NC,OR]
# Block all http and https referrals from semalt.com” and all subdomains of “semalt.com”
RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)*semalt\.com\ [NC,OR]
# Block all http and https referrals from “kambasoft.com” and all subdomains of “kambasoft.com”
RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)*kambasoft\.com\ [NC]
RewriteRule .* – [F]

# block visitors referred from semalt.com
RewriteEngine on
RewriteCond %{HTTP_REFERER} semalt\.com [NC]
RewriteRule .* – [F]
 
I have an .htaccess tip that is a bit different.

Keep in mind that loading up your .htaccess file with a lot of rules can be a lot, lot slower on server performance than putting these exact same rules in your httpd.conf or even running them through a PHP script. If you notice unusual hangs or load times you may have an overloaded .htaccess and if you have access to httpd.conf it may be worth using this instead.
 
Don't do Apache, but if anyone uses Nginx, throw these at the top of your configuration file(s) for each domain:

Code:
set $block_sql_status 0;
if ($query_string ~ "union.*select.*\(") { set $block_sql_status 1; }
if ($query_string ~ "union.*all.*select.*") { set $block_sql_status 1; }
if ($query_string ~ "concat.*\(") { set $block_sql_status 1; }
if ($block_sql_status = 1) { return 403; }

set $block_file_status 0;
if ($query_string ~ "[a-zA-Z0-9_]=http://") { set $block_file_status 1; }
if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") { set $block_file_status 1; }
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") { set $block_file_status 1; }
if ($block_file_status = 1) { return 403; }

set $block_common_status 0;
if ($query_string ~ "(<|%3C).*script.*(>|%3E)") { set $block_common_status 1; }
if ($query_string ~ "base64_(en|de)code\(.*\)") { set $block_common_status 1; }
if ($block_common_status = 1) { return 403; }
 
I have an .htaccess tip that is a bit different.

Keep in mind that loading up your .htaccess file with a lot of rules can be a lot, lot slower on server performance than putting these exact same rules in your httpd.conf or even running them through a PHP script. If you notice unusual hangs or load times you may have an overloaded .htaccess and if you have access to httpd.conf it may be worth using this instead.

Having a long httpd.conf will also slow down your server quite a bit.

I used to own several free hosting companies and we experimented with all sorts of different setups. One of the absolute worst setups we had was giving every user a cPanel account because cPanel adds to the httpd.conf for every account and it didn't take long before Apache began to collapse under its own weight largely due to it having to read and process a very large httpd.conf every time a page was loaded.
 
Block referrer spam

RewriteEngine On

# Garbage collection

RewriteCond %{HTTP_REFERER} (savetubevideo\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (srecorder\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (semalt\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (kambasoft\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (7makemoneyonline\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (netwasgroup\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (nic4u\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (wear4u\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (foxmediasolutions\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (liveplanets\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (aeterna-tech\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (continentaltirebowl\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (chemsymphony\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (infolibria\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (globaleducationeurope\.net) [NC,OR]

RewriteCond %{HTTP_REFERER} (soma\.125mb\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (mitglied\.lycos\.de) [NC,OR]

RewriteCond %{HTTP_REFERER} (foxmediasolutions\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (jroundup\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (feathersandfurvanlines\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (conecrusher\.org) [NC,OR]

RewriteCond %{HTTP_REFERER} (sbj-broadcasting\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (edthompson\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (codychesnutt\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (artsmallforsenate\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (axionfootwear\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (protzonbeer\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (candiria\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (bigsitecity\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (coresat\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (istarthere\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (amateurvoetbal\.net) [NC,OR]

RewriteCond %{HTTP_REFERER} (alleghanyeda\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (xadulthosting\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (datashaping\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (zick\.biz) [NC,OR]

RewriteCond %{HTTP_REFERER} (newprinceton\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (dvdsqueeze\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (xopy\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (webdevboard\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (devaddict\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (eaton-inc\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (whiteguysgroup\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (guestbookz\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (webdevsquare\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (indfx\.net) [NC,OR]

RewriteCond %{HTTP_REFERER} (snap\.to) [NC,OR]

RewriteCond %{HTTP_REFERER} (2y\.net) [NC,OR]

RewriteCond %{HTTP_REFERER} (astromagia\.info) [NC,OR]

RewriteCond %{HTTP_REFERER} (fbdownloader\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (descargar-musicas-gratis\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (baixar-musicas-gratis\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (ilovevitaly\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (econom\.co) [NC,OR]

RewriteCond %{HTTP_REFERER} (acunetix-referrer\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (yougetsignal\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (darodar\.com) [NC,OR]

RewriteCond %{HTTP_REFERER} (buttons-for-website\.com) [NC]

RewriteRule .* - [F]
 
set mime types for html5 video support:

Code:
AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm
 
Code:
# Block Bots
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^rogerbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^exabot [OR]
RewriteCond %{HTTP_USER_AGENT} ^MJ12bot [OR]
RewriteCond %{HTTP_USER_AGENT} ^dotbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^gigabot [OR]
RewriteCond %{HTTP_USER_AGENT} ^AhrefsBot
RewriteRule .* - [F]

SetEnvIfNoCase User-Agent .*rogerbot.* bad_bot
SetEnvIfNoCase User-Agent .*exabot.* bad_bot
SetEnvIfNoCase User-Agent .*mj12bot.* bad_bot
SetEnvIfNoCase User-Agent .*dotbot.* bad_bot
SetEnvIfNoCase User-Agent .*gigabot.* bad_bot
SetEnvIfNoCase User-Agent .*ahrefsbot.* bad_bot
SetEnvIfNoCase User-Agent .*sitebot.* bad_bot
<Limit GET POST HEAD>
Order Allow,Deny
Allow from all
Deny from env=bad_bot
</Limit>
</IfModule>
# END Block Bots