Troy297
06-09-2007, 04:28 AM
Hey Guys,
Just wondering if it's possible with htaccess to say disable viewing of all text files on my server? Like they are still accessible if a script calls them but not if you just type out the direct url into your browser.
If so how might I do this?
Thanks again! +Rep for replies! :thumbsup:
_Aerospace_Eng_
06-09-2007, 07:50 PM
You can use this
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://yoursite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://yoursite.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yoursite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yoursite.com$ [NC]
RewriteRule .*\.(txt)$ - [F,NC]
You can prevent other files from being accessed by typing in the url by adding a | and the file extension. For example this will prevent hotlinking of your images as well
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://yoursite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://yoursite.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yoursite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yoursite.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|txt)$ - [F,NC]
Troy297
06-09-2007, 10:12 PM
Thanks for the code except one thing.....
When I tried using it I got a 500 error :S anyone know why? Thxs :)
_Aerospace_Eng_
06-09-2007, 10:19 PM
What kind of server are you using? Apache or Windows? Did you actually change yoursite.com to your url?
Troy297
06-10-2007, 12:26 AM
Apache on Windows
Yes I changed url.
_Aerospace_Eng_
06-10-2007, 12:32 AM
Yeah I've had problems using htaccess with apache on Windows. Are you by chance using XAMPP?
_Aerospace_Eng_
06-10-2007, 12:39 AM
Yeah I was never able to get an htaccess file to work with xampp. Aha! Just did some googling. It turns our mod_rewrite isn't turned on by default. Article that says to get it enabled here
http://www.mydigitallife.info/2006/03/08/wordpress-permalinks-does-not-work-in-xampp-setup/
The article discusses permalinks but I think using the advice from the article might make htaccess file on xampp work as they should. *Goes to test out the theory*
Troy297
06-10-2007, 12:45 AM
Okay thxs :)
I'll take a look :D
_Aerospace_Eng_
06-10-2007, 12:51 AM
Yep after following the advice in that article, htaccess worked as it should, got no 500 error and hotlinking of txt files was disabled. In the latest version of XAMPP 1.6.2 you only have to enable the mod_rewrite module. You may have to change the code a bit to look something like this
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://localhost/folderwherefilesareat/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://localhost/folderwherefilesareat$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|txt)$ - [F,NC]
Since its your localhost, I don't know.