Mudasser Ajaz
Mudasser Ajaz

Reputation: 6257

Protect subdirectory with .htaccess and .htpasswd

I have a directory memories and there is a subdirectory in it /memories/application/controllers/admin.
I want to protect admin directory using .htaccess and .htpasswd.

This is my .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    AuthUserFile /var/www/memories/application/controllers/admin/.htpasswd
    AuthType Basic
    AuthName "memories with mom"
    Require admin
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

# Various security things
ServerSignature off
php_value expose_php Off
php_value  session.cookie_httponly On
Options -Indexes
ErrorDocument 403 /404.php

But it is protecting the whole parent directory, whereas I only want the subdirectory admin to be protected. My .htpasswd is also in admin folder.

Upvotes: 3

Views: 3794

Answers (1)

johnh10
johnh10

Reputation: 4185

If you only want the subdirectory admin protected by the .htpasswd file then only put the auth specific htaccess code in that subdirectory and not the .htaccess for the whole site.

/var/www/memories/application/controllers/admin/.htaccess should only be

AuthUserFile /var/www/memories/application/controllers/admin/.htpasswd
AuthType Basic
AuthName "memories with mom"
Require admin

and remove the above code from your main .htaccess

Upvotes: 1

Related Questions