如何使用 htaccess 限制对条目的访问?

发布于 2024-11-18 20:08:26 字数 422 浏览 3 评论 0原文

我需要用密码保护网站上的几个条目。在模板级别很容易做到,但这是入门​​级别。我正在运行表达式引擎。

我尝试设置 htaccess 文件,但尚未生效。

它是这样的:

AuthName "Restricted Area" 
AuthType Basic 
AuthUserFile /home/server/.htpasswds/.htpasswd 
AuthGroupFile /dev/null 
<Files template_group/entry_name>
require valid-user
</Files>

其中 template_group 是实际 template_group 的名称,entry_name 是条目的实际名称。

任何帮助将不胜感激。

谢谢。

I need to password protect a couple of entries on a site. It is easy to do at the template level but this is at the entry level. I am running Expression Engine.

I tried setting up an htaccess file but it is not yet effective.

It is like this:

AuthName "Restricted Area" 
AuthType Basic 
AuthUserFile /home/server/.htpasswds/.htpasswd 
AuthGroupFile /dev/null 
<Files template_group/entry_name>
require valid-user
</Files>

Where template_group is the name of the actual template_group and entry_name is the actual name of the entry.

Any assistance will be appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-11-25 20:08:26

虽然 ExpressionEngine 提供了自己的模板访问限制方法来密码保护页面和模板 - 包括处理 .htaccess Apache Basic HTTP 身份验证 - 在某些情况下您可能不想或无法使用它:

如果您属于 DIY 类型,则可以修改 httpd.conf 以限制和密码保护对 ExpressionEngine 页面、条目和模板的访问

此技术的工作原理如下:

  1. 编辑 Apache 的 httpd.conf
  2. 创建 .htpasswd.htgroup 文件
  3. 指定要保护的 URL

注意:由于我们尝试在 URL 级别而不是物理文件系统上匹配对象,因此我们必须使用 指令1

将以下内容放入服务器的httpd.confvhost.conf文件中:

<LocationMatch "^/private">
    AuthName "Restricted Area"
    AuthType Basic
    AuthUserFile /path/to/website/.htpasswd
    AuthGroupFile /dev/null
    Require valid-user
</LocationMatch>

请务必更改根据您的喜好和托管环境的指令。

如果您还没有创建 .htpasswd 密码文件来加密所需的密码,可以使用命令行或 在线 .htaccess 密码生成器:

htpasswd -c /path/to/website/.htpasswd username

如果 htpasswd 命令不在您的 Unix 路径,您必须输入该文件的完整路径才能运行它。在我的服务器上,它将是:

/usr/sbin/htpasswd -c /path/to/website/.htpasswd username

然后,htpasswd 会询问您用户的密码,并要求您输入它再次确认:

# htpasswd -c /path/to/website/.htpasswd username
New password: changeme
Re-type new password: changeme
Adding password for user username

一切就位并正常工作后,任何对 /private* 的请求都将由 Apache 处理,然后再路由到 ExpressionEngine。


瞧 — Apache受密码保护的目录与 ExpressionEngine(或任何 CMS,例如 WordPress、MovableType 或 TextPattern)协调工作。

  1. 上下文 指令的 指定它只能在 服务器配置虚拟主机 配置文件中使用。这意味着我们不能将规则放入 .htaccess 文件中,否则 Apache 将抛出 500 内部服务器错误,并描述为“此处不允许位置”。

    • 如果您尝试在 URL 级别匹配对象,则必须使用
    • 如果您尝试在文件系统级别匹配对象,则必须使用 和/或



While ExpressionEngine provides its own means for Template Access Restriction to password-protect pages and templates — including handling .htaccess Apache Basic HTTP Authentication — there are situations where you might not want to, or are unable, to use it:

  • For example, the Freelancer Version of ExpressionEngine doesn't include the Member Management Module, so the Template Preferences Manager doesn't offer Access Restrictions.

  • Also, if you elect to use ExpressionEngine's HTTP Authentication, only users with member accounts [in ExpressionEngine] will be able to login, since EE uses its local member database for authentication.

If you're the DIY type, you can modify your httpd.conf to limit and password-protect access to ExpressionEngine pages, entries and templates.

This technique works by:

  1. Editing Apache's httpd.conf
  2. Creating .htpasswd or .htgroup files
  3. Specifying the URL(s) to Protect

Note: Since we are attempting to match objects at the URL level and not the physical filesystem, we must use a <Location> or <LocationMatch> directive1.

Put the following in your server's httpd.conf or vhost.conf file:

<LocationMatch "^/private">
    AuthName "Restricted Area"
    AuthType Basic
    AuthUserFile /path/to/website/.htpasswd
    AuthGroupFile /dev/null
    Require valid-user
</LocationMatch>

Be sure to change the values of the directive to your liking and your hosting environment.

If you haven't already, create the .htpasswd password file to encrypt the desired passwords, either using the command line or an Online .htaccess Password Generator:

htpasswd -c /path/to/website/.htpasswd username

If the htpasswd command is not in your Unix path, you'll have to type the full path to the file to get it to run. On my server, it would be:

/usr/sbin/htpasswd -c /path/to/website/.htpasswd username

Then, htpasswd will ask you for the user's password, and ask you to type it again to confirm:

# htpasswd -c /path/to/website/.htpasswd username
New password: changeme
Re-type new password: changeme
Adding password for user username

With everything in place and working, any request to /private* will be handled by Apache before it's routed to ExpressionEngine.


Voilà — Apache password-protected directories working in harmony with ExpressionEngine (or any CMS really, such as WordPress, MovableType or TextPattern).

  1. The context of the <Location> directive specifies that it can only be used in server config and virtual host configuration files. This means we can't put the rules in a .htaccess file, otherwise Apache will throw a 500 Internal Server Error with the description "Location not allowed here".

    • If you are attempting to match objects at the URL level, you must use <Location>
    • If you are attempting to match objects at the filesystem level, you must use <Directory> and/or <Files>
无需解释 2024-11-25 20:08:26

我回答了两个类似 关于此主题的问题可能有益于你。

不过,有多种方法可以对 ExpressionEngine 站点中的页面进行密码保护:

  • 模板 首选项管理器 条件
  • 全局变量
  • 第三方加载项

到目前为止,针对您的情况最简单的解决方案是使用内置的 ExpressionEngine 控制面板中的模板首选项管理器 并分配“私有”条目到需要身份验证的模板。

第三方附加组件,例如 Yuri Salimovskiy 的 Entry Access IntoEEtive 可能会对您有所帮助。 条目访问使您能够限制某些成员或成员组对某些频道条目的前端访问。

I answered two similar questions on this subject that may be of benefit to you.

Nevertheless, there are several ways to password-protect pages in an ExpressionEngine site:

  • Template Preferences Manager
  • Conditional Global Variables
  • Third-Party Add-Ons

By far the easiest solution to your situation is to use the built-in Template Preferences Manager in the ExpressionEngine Control Panel and assign the "private" entries to a template that requires authentication.

A third-party add-on such as Entry Access by Yuri Salimovskiy of IntoEEtive may aide in your benefit. Entry Access enables you to restrict front-end access to certain channel entries for certain members or member group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文