繁体   English   中英

在WordPress中使用htaccess限制对一页的访问

[英]restrict access to one page using htaccess in wordpress

我在Wordpress theme_directory/page-rsvp.php中有一个页面,我需要受.htaccess保护的密码。 我正在使用MAMP PRO在本地进行开发。

我的theme_directory中有一个.htaccess和.htpasswd文件。 我在.htaccess文件中包含以下内容,但无法正常工作。 每当我转到localhost:8888 / site / rsvp时,身份验证提示都不会显示。

 <Files “page-rsvp.php”>
   AuthName "Username and password required"
   AuthUserFile ./.htpasswd
   Require valid-user
   AuthType Basic
 </Files>

当然,wordpress安装根目录中的.htaccess文件如下所示:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /thepowerfulnow/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /thepowerfulnow/index.php [L]
</IfModule>

# END WordPress

如何仅使用第一个.htaccess文件(theme_directory中的文件)才能使它工作? 我需要使这个主题模块化。 我对.htaccess文件非常陌生。 谢谢你们!

您已将身份验证添加到主题目录中,因此仅当使用以下URL直接访问主题文件时,身份验证才有效:

http://localhost:8888/site/wp-content/themes/theme_directory/page-rsvp.php

如果要限制对页面URL的访问(例如/site/rsvp ),则需要使用其他规则。 您可以将以.htaccess添加到主.htaccess文件的末尾,即WordPress根目录中。

# Set an env variable "auth" if the URI starts with "/site/rsvp"
SetEnvIf Request_URI ^/site/rsvp auth=1

AuthName "User and password required"
AuthType Basic
AuthUserFile "./.htpasswd"

# First, allow anyone
Order Allow,Deny
Satisfy any
Allow from all
Require valid-user
# Then, deny only if auth is required
Deny from env=auth

请记住,将.htpasswd文件的副本保留在同一目录中,或者使用AuthUserFile指令将文件的路径设置为该路径。

替代解决方案

我的原始答案使用了<Location>指令来过滤要限制的URL,但它不适用于.htaccess文件 仅在服务器配置或虚拟主机下。 在此处检查上下文http : //httpd.apache.org/docs/2.2/mod/core.html#location

如果要使用以下限制规则,并且您具有访问服务器的权限,则需要将其添加到某些上下文中(服务器配置或虚拟主机)。 Apache的配置文件通常位于/etc/httpd/etc/apache2目录中。

<Location "/site/rsvp"> # Where /site/rsvp is the URL you want to restrict
    AuthName "Username and password required"
    AuthUserFile /path/to/.htpasswd
    Require valid-user
    AuthType Basic
</Location>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM