2011-04-12 59 views
0

我的WordPress安装有一个特定的页面(这是WordPress中的一个实际页面,只有漂亮的URL),我想用提供给我的.htaccess文件进行密码保护。使用.htaccess保护特定的WordPress页面

例如,页面是http://www.myawesomewebsite.com/members。我只想要一些用户访问它,他们的用户和密码存储在.htpasswd文件中。

.htaccess文件发送到我的客户端是

AuthUserFile /put the path to the password file here 
AuthGroupFile /dev/null 
AuthName RESTRICTED 
AuthType Basic 

<Limit GET POST> 
require valid-user 
</Limit> 

我需要使用这个参数来保护这个WordPress页面。我疯了,我的搜索没有任何结果。任何猜测?

回答

0

您只需将.htaccess文件放入“members”目录中,然后将第一行指向密码文件即可。要创建密码文件只是做:

htpasswd -c MyPasswordFile username 
htpasswd MyPasswordFile anotherusername 

这里有一个很好的参考:

http://httpd.apache.org/docs/current/howto/htaccess.html

+1

我需要保护一个WordPress页面。这不是一个文件夹。 – 2011-04-13 19:03:48

0

WordPress允许您使用密码来保护页/职位(这是相当easyer走这条路线比使用htaccess):转到wp-admin,编辑您想保持私密但允许访问某些用户的页面/文章,并在“预览更改”按钮下面,您会看到“状态:发布编辑”并获得它“可见性:公共编辑”点击可见性编辑,选择密码保护无线电框(或私人的),输入密码a nd打OK。你应该很好走。

+0

我需要通过.htaccess来完成。不可能? – 2011-04-13 19:04:18

+0

你为什么需要使用.htaccess?我不明白这一点,如果你能做到这一点,我能想到的唯一的事情就是拥有不同密码的多个用户,但是这可以通过私人博客/帖子再次获得,而不是使用htaccess。看看它在这里的行动http://wordpress.tv/2009/01/14/setting-your-posts-or-entire-blog-to-private/和http://en.support.wordpress.com/posts/后知名度/ – 2011-04-13 20:14:20

1

我能够做到这一点:

  1. 创建由保护的.htaccess文件系统上的目录结构。
  2. 将自定义字段添加到任何我想要保护的页面,该页面指定使用哪个目录来保护它。
  3. 修改page.php模板以限制对页面的访问,具体取决于用户是否可以访问自定义字段指定的目录。

具体步骤如下:

  1. 创建的wp-content目录中的目录 “组”。
  2. 创建包含以下内容的wp-content /组/ index.php文件:

    <?php 
    require(dirname(__FILE__) . '/../../wp-load.php'); 
    class CASGroupAuth { 
        static function authenticate(){ 
         $groups = @$_SESSION['cas-groups']; 
         if (!$groups) $groups = array(); 
         $groupdir = basename(dirname($_SERVER['PHP_SELF'])); 
         $groups[$groupdir] = true; 
         $_SESSION['cas-groups'] = $groups; 
         if ([email protected]$_GET['redirect_to']){ 
          die("You didn't provide a redirect"); 
         } 
         header('Location: '.$_GET['redirect_to']); 
         exit; 
        } 
    } 
    CASGroupAuth::authenticate(); 
    
  3. 的 “组” 目录下创建一个子目录 “MYGROUP”。
  4. 创建一个到mygroups目录下的groups/index.php的符号链接。即

    $ cd mygroup 
    $ ln -s ../index.php index.php 
    
  5. 添加.htaccess文件到您的组/ MYGROUP目录适当的访问限制。在我的情况下,我使用CAS apache模块,所以我的。htaccess文件看起来像:

    AuthType CAS 
    require sfu-user shannah !my-maillist 
    
  6. 修改的page.php文件模板,我的主题(即里面的wp-content /主题/ mytheme的/)是:

    <?php if (have_posts()) : the_post(); 
    $group = get_post_meta(get_the_ID(), 'cas-group', true); 
    if (trim($group)){ 
        $group = trim($group); 
        $existingGroups = @$_SESSION['cas-groups']; 
        if (!$existingGroups or [email protected]$existingGroups[$group]){ 
         nocache_headers(); 
         header('HTTP1.1 302 Moved Temporarily'); 
         header('Location: ' . get_settings('siteurl') . '/wp-content/groups/'.basename($group).'/index.php?redirect_to='.urlencode($_SERVER['REQUEST_URI'])); 
         header('Status: 302 Moved Temporarily'); 
         exit; 
        } 
    } 
    
    ?> 
    <?php get_header(); ?> 
    
        <div id="content" class="narrowcolumn"> 
    
    
         <div id="PageTitle"><!-- TemplateBeginEditable name="PageTitle" --><?php the_title(); ?><!-- TemplateEndEditable --></div> 
         <div class="post" id="post-<?php the_ID(); ?>"> 
          <div class="entry"> 
           <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?> 
    
    
    
          </div> 
         </div> 
    
        <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?> 
        </div> 
    
    
    <?php get_footer(); ?> 
    <?php endif; ?> 
    

重要其中一部分是顶部的部分,因为代码用于检查CAS组自定义字段并重定向用户,需要在任何输出发送到浏览器之前运行。为此,我重新安排了if(has_posts())语句的位置以覆盖整个页面的位置,而不仅仅是内容 - 就像它在默认模板中一样。

此时,如果您将自定义字段添加到名称为“cas-group”并且值为“mygroup”的wordpress网站的任何页面,那么对该页面的访问权限将被适当限制为有权访问您的用户groups/mygroup目录基于其.htaccess文件中的规则。

如果你想拥有不同的组,你可以创建一个mygroup目录的副本并修改其中的.htaccess文件限制。

+0

这是一个聪明的解决方案。它会在一次登录后提供对该组所有受保护URL的访问权限吗? – supertrue 2012-08-14 14:01:04