Only allow specific referrers with .htaccess

Apr032007 2:30AM — John

Say you have a secret directory full of stuff you don’t want anyone to see and you want to allow certain domains to have access to it directly. All you would need to do is create (or add this to) a .htaccess file in the root of the directory you want to protect.

You would do it thusly:
Add this to a blank or your existing .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?first-domain-to-allow.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?second-domain-to-allow.com/.*$ [NC]
RewriteRule ^(.*)$ http://www.google.com/ [R,L]

Also, if you want people who copy and paste/type in the link to have access, all you would need to do is add this line just after RewriteEngine on so your .htaccess file would look like this…
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?first-domain-to-allow.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?second-domain-to-allow.com/.*$ [NC]
RewriteRule ^(.*)$ http://www.google.com/ [R,L]

Advertising

No Comments

Leave a comment


Post Info

Filed In:
Tips & Tricks

Tags:
,

Permalink:

Related Posts

  • No Related Post

Free iTunes Song

Apple iTunes
Advertising