.htaccess,mod_rewrite和基本身份验证

时间:2012-08-30 19:21:57

标签: wordpress .htaccess mod-rewrite basic-authentication

我正在使用Wordpress网站,我的网页正在使用永久链接结构,mod_rewrites它们看起来像目录。对于几页我想使用基本身份验证来密码保护一些页面。我如何在我的.htaccess文件中写这个?我保护文件或重写的地址吗?

3 个答案:

答案 0 :(得分:7)

你不需要mod_rewrite,希望这应该可以解决问题:

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内容以满足您的需求。

答案 1 :(得分:1)

我对此解决方案的唯一问题是单击取消按钮将显示受保护的页面。我尝试使用:

来解决这个问题
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>'

答案 2 :(得分: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”最后合并)

感谢comment from Alek指出

所以当我删除I​​fModule括号时,规则已经开始为我工作了。