Forum Moderators: phranque

Message Too Old, No Replies

Protecting folder with .htaccess

Problem when trying to protect a folder with .htaccess.

         

seo sitemaker

5:27 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



Hello,

I have a folder in the root (eg. domain.com/script/), from where I am running a script that accesses the database for backup purposes. I am trying to protect it with .htaccess:

AuthName "Script"
AuthType Basic
AuthUserFile "/home/path/to/.htpasswd"
require valid-user


However, after making those files, when I try to go to the directory, I get a 404.

I have figured out that there is some kind of interference from a different .htaccess which is in the root (eg. domain.com/.htaccess), it is there for a wordpress site. When I delete it, this one works fine.

So my question is, should I just add this code to the .htaccess in the root and specify the folder, and if so how do I do that? Or is there something else that I should be doing?

Thanks.

jdMorgan

4:55 am on Aug 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot just "add this code to the root .htaccess" -- There's really no way to "specify the folder" to be protected in that case. You could use a <Directory> container for this purpose in a server config file, but that is not an option if you are limited to .htaccess files only.

However, you should carefully review all the code in your root .htaccess file, and specifically exclude the /script/ directory-paths from being rewritten by any rule in that .htaccess file.

You can either add exclusions to all relevant rules, as in
 RewriteCond %{REQUEST_URI} !^/script/ 

or you can use a 'skip rule' or a 'quit rule' to skip over or terminate mod_rewrite processing if the /script/ path is requested. See Apache mod_rewrite Rewrite Rule [S=nnn] and [L] flag descriptions. Examples:

# Quit processing rules here if the request is for anything in the /script/ subdirectory
RewriteRule ^script/ - [L]
-or-
# Skip the next four rules if the request is for anything in the /script/ subdirectory
RewriteRule ^script/ - [S=4]


Jim