htaccess Rewrite Cheatsheet

CODE:
# Site has permanently moved to new domain
# domain1.com to domain2.com
RewriteCond %{HTTP_HOST} ^www.domain1.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]

CODE:
# Page has moved temporarily
# domain.com/old_page.html to domain.com/new_page.html
RewriteRule ^old_page.html$ new_page.html [R,NC,L]

CODE:
# SEO / Pretty URLs (no query string)
# domain.com/category-name-1/ to domain.com/categories.php?name=category-name-1
RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?name=$1 [L]

CODE:
# Pretty URLs with pagination (no query string)
# domain.com/section/title/5 to domain.com/section.php?name=title&page=5
RewriteRule ^section/([A-Za-z0-9-]+)/([0-9]+)/?$ section.php?name=$1&page=$2 [L]

CODE:
# Block referrer spam
RewriteCond %{HTTP_REFERRER} (casino) [NC,OR]
RewriteCond %{HTTP_REFERRER} (poker) [NC]
RewriteRule .* – [F]

CODE:
# Block image hotlinking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*.(jpe?g|gif|bmp|png)$ http://domain.com/nohotlink.gif [L]

CODE:
# Block image hotlinking from myspace
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+.)?myspace.com/ [NC,OR]
RewriteRule .*.(jpe?g|gif|bmp|png)$ http://domain.com/nohotlink.gif [L]

Leave a Reply

Your email address will not be published. Required fields are marked *