Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
8 total  XML / RSS feed 

Protect .svn directories using htaccess

// block access to .svn dirs
// should be done server-wide if you can (another snippet)

<IfModule mod_rewrite.c>
  RewriteRule ^(.*/)?\.svn/ - [F,L]
  ErrorDocument 403 "Access Forbidden"
IfModule>

Protect .svn directories server-wide (Apache)

// protect ".svn" and "CVS" dirs (could add more)
// for server-wide protection; goes in httpd.conf
// there's a separate snippet for .htaccess-based code

<DirectoryMatch "^/.*/(\.svn|CVS)/">
  Order deny,allow
  Deny from all 
DirectoryMatch>

Maintenance mode for apache

Doing some work on your web site but you don't want other people to see what you're doing. Give them a redirect (like the flickr massage screen).
You need to use your own ip of course.

# redirect all visitors to alternate site but retain full access for you
ErrorDocument 403 http://www.alternate-site.com
Order deny,allow
Deny from all
Allow from 99.88.77.66

301 Permanent Redirect rule to consolidate domains

// redirect domain.com to www.domain.com (or vice versa)
// helps substantially with delicious links and other SEO optimization
// see for discussion

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

'Site is under maintenance' redirect, except for you

Upgrades woohoo! Tell those lusers to hold their horses while you happily debug. Some .htaccess magic, just sub in your IP:

<IfModule mod_rewrite.c>
        RewriteEngine On
        # 'under construction' override
        RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
        RewriteRule ^(.*)$ maintenance.html
IfModule>

Force or eliminate the www prefix for a domain

// Always add the www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 


// Always eliminate the www prefix, use this htaccess code.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Block Blacklisted Bandwidth Thieves from Hotlinking Your Image Files

Code for an .htaccess file located in a directory containing images (or any other type of file for that matter) to which you want to block hotlinking from blacklisted websites.
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(www\.)?evilwebsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://forum\.evilwebsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?bandwidththieves\.net [NC]
RewriteRule \.(jpeg|jpg|gif|bmp|png|JPEG|JPG|GIF|BMP|PNG)$ http://example.com/pwnt.gif [L]

example.com/pwnt.gif
is the location of the file you want to serve in place of the hotlinked image. Mine just says 'PWNT'.

Alternate last line to simply block the images from being hotlinked instead of serving a replacement image:
RewriteRule \.(jpeg|jpg|gif|bmp|png|JPEG|JPG|GIF|BMP|PNG)$ - [F]

mod_rewrite rules to serve application/xhtml+xml

The following mod_rewrite rules will serve HTML files as application/xhtml+xml to supporting browsers.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - [T=application/xhtml+xml]


Source: Mark Pilgrim, The Road to XHTML 2.0: MIME Types.
« Newer Snippets
Older Snippets »
8 total  XML / RSS feed