【问题标题】:restrict access to one page using htaccess in wordpress在 wordpress 中使用 htaccess 限制对一页的访问
【发布时间】:2015-12-03 10:28:54
【问题描述】:

我在 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 文件很陌生。谢谢大家!

【问题讨论】:

  • 你试过用"代替非ascii的吗?
  • 好收获!我需要新眼镜 :( 不幸的是,这并没有解决。
  • 甚至尝试删除引号但也没有解决它。任何帮助表示赞赏!谢谢!

标签: php wordpress apache .htaccess


【解决方案1】:

您已将身份验证添加到您的主题目录,因此只有当您使用这样的 URL 直接访问您的主题文件时它才会起作用:

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

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

# 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 指令设置该文件的路径。

替代解决方案

我的原始答案使用&lt;Location&gt; 指令过滤要限制的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>

【讨论】:

  • 感谢您的详细回复。我已经尝试了您的两种解决方案,但都无法正常工作。我是 .htaccess 的新手,但我的猜测是它可能与我在 OP 中提供的第二个 .htaccess 文件中的某些内容有关。因为它重新配置了网站的永久链接。使用您的第一个解决方案,我将“site”替换为适当的文件夹名称,即站点名称,并将“theme_directory”替换为主题名称,但没有运气,但这并没有破坏我网站的 css。第二种解决方案我尝试了相对路径和绝对路径,但都破坏了我网站的 css。请问有什么建议吗?谢谢!!
  • 只是为了确认一下,您是否尝试将 &lt;Location&gt; 指令添加到您的主 .htaccess 中?
  • 如果你的主要意思是theme_directory中的那个,那么是的。我还没有在我的 wordpress 安装的根目录中尝试过这个。我应该吗?
  • 是的。 Wordpress 根目录中的那个是您的主要.htaccess。尝试将其添加到文件末尾,在注释后的新行中:# END WordPress
  • 感谢您的回复!我尝试了您的建议,但它会产生 500 内部服务器错误。我希望网址有误。我需要限制的文件的完整路径是/applications/mamp/htdocs/thepowerfulnow/wp-content/themes/thepowerfulnow/page-rsvp.php,根.htaccess 文件在applications/mamp/htdocs/thepowerfulnow。我提供的这个 .htaccess 文件的 url 是 &lt;Location "/thepowerfulnow/rsvp"&gt;&lt;Location “./wp-content/themes/thepowerfulnow/rsvp"&gt; 但都返回相同的 500 内部服务器错误。这可能是因为 MAMP 吗?帮助表示赞赏!谢谢大家!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2015-10-07
相关资源
最近更新 更多