How to use .htaccess on subfolders with Drupal

If you are trying to password protect a subdirectory of your Drupal website using the traditional .htaccess and .htpassword method, you might be having trouble accessing the contents of the subdirectory. When trying to navigate to a page under the subfolder, Drupal simply displays it as not found.

There is a very easy fix for this. Simply add this line at the top of your .htaccess file:

ErrorDocument 401 Unauthorized

This must be done for every single subdirectory containing the .htaccess and .htpasswd files.

Your final .htaccess file would look something like this:

ErrorDocument 401 "Unauthorized"
        AuthUserFile /locationofyoursubdirectory/.htpasswd
        AuthGroupFile /dev/null
        AuthName "Secure Document"
        AuthType Basic
        
        require user user1
        require user user2
        require user user3
        require user user4
        require user user5

After inserting that small line at the top of your .htaccess file, your subdirectories should be password protected but accessible, like intended.

6 Comments on How to use .htaccess on subfolders with Drupal

6 Responses to How to use .htaccess on subfolders with Drupal

  1. Alex  says: October 30, 2009 at 6:04 pm

    THANK YOU! Wow Ive spent a couple frustrating days on this problem, and I am so glad I came across this!

    Very nice clear post.

    Reply
  2. choopong  says: January 10, 2010 at 8:53 pm

    thank you so much :)

    Reply
  3. Drupal Developers  says: May 20, 2011 at 5:13 am

    What to do in case of non www to www redirects when individual sub folders have their own .htaccess files? The global .htaccess changes are ignored because of the commented code in the folder level .htaccess. When we uncomment the code, it redirects to the www of the parent folder rather than the www of the same.

    Reply
  4. Ajgor says: June 26, 2011 at 5:54 am

    ITS COOL

    thank you so much :)

    Reply
  5. JonoB says: December 17, 2011 at 3:58 pm

    Thank you sorted my problem out straight away!

    Reply
  6. Chris says: July 8, 2018 at 6:27 am

    This was bugging me for YEARS!

    Thank you so much!

    Reply

Contact Us

Contact us via phone (+1 866-383-1657) or leave a detailed message below for sales, support, or any other needs

*Required Field
*Required Field
I'd like to receive the newsletter. *Check email for confirmation.
*Required Field
8:00am - 12:00pm 12:00pm - 5:00pm