htaccess

Add www subdomain automatically to your domain with htaccess

Posted by on Aug 9, 2010 in Backend | No comments

Sometimes we need to add the www to our domain automatically, maybe for aesthetics or ajax issues across subdomains. Apache .htaccess is our friend here. You just need to add this to your existing .htaccess file or just make a new one with the following contents:

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

Prevent image hotlinking with Apache and htaccess

Posted by on Aug 9, 2010 in Backend | No comments

Question of the million dollars. Those annoying image hot-linkers, eating our bandwidth, our food, our… nevermind. Anyway, if you want to give them hell and stop this behaviour, add the following to your .htaccess file (remember to edit your domain name):

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://your-server-ip/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://my-domain.com/.*$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.my-domain.com/.*$  [NC]
RewriteRule .*.(gif|GIF|jpg|JPG|png|PNG)$ - [F]

Bonus!: In the third line, you can optionally write your server IP for an extra layer of security. If you don’t know your server’s IP, just open an MS-DOS console (if you’re on Windows) or a Linux console and ping your domain. ie. “ping dannyherran.com”, would return 67.220.192.149 which is my server IP.