.htaccess, mod_rewrite and basic authentication

I am working on a Wordpress site, and my pages use a permalink structure that mod_rewrites to look like directories. For multiple pages, I want to use Basic Authentication to protect passwords on multiple pages. How can I write this in my .htaccess file? Am I protecting a file or a rewritten address?

+5
source share
3 answers

You won't need mod_rewrite to do this, hope this should do the trick:

SetEnvIfNoCase Request_URI ^/some/path/to/protect require_auth=true
SetEnvIfNoCase Request_URI ^/another/protected/path require_auth=true

# Auth stuff
AuthUserFile /var/www/htpasswd
AuthName "Password Protected"
AuthType Basic

# Setup a deny/allow
Order Deny,Allow
# Deny from everyone
Deny from all
# except if either of these are satisfied
Satisfy any
# 1. a valid authenticated user
Require valid-user
# or 2. the "require_auth" var is NOT set
Allow from env=!require_auth

mod_auth mod_env mod_rewrite, . SetEnvIfNoCase Request_URI ^/some/path/to/protect require_auth=true , auth .

+7

, . , :

RewriteCond %{REMOTE_USER} !user
RewriteRule ^/protected-page /unauthenticated-page [R=401]

. , .

,

ErrorDocument 401 "You don't have access."

,

ErrorDocument 401 '<html><head><meta http-equiv="refresh" content="0; url=/unauthenticated-page" /></head><body></body></html>'
0

, , , .htaccess

AuthType Basic
AuthName "some_name"
AuthUserFile "/path/to/password/passwd"
require valid-user
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

, (, )

- ( "If" )

Alek,

so when i removed the brackets of IfModule, the rules started to work for me.

0
source

All Articles