WordPress.org

Ready to get started?Download WordPress

Forums

Member Access
[resolved] Problems with MemberAccess and htaccess rewrite rule (3 posts)

  1. jackzelig
    Member
    Posted 1 year ago #

    TLDR;
    My WP installation rewrites all URLs to omit the "www". However, when using MemberAccess, entering any URL containing a "www" always redirects me to the dashboard (rather than the page I was trying to access) after a successful login. Help!

    Longer Version:
    --------------
    In my WP backend, my WP address and my site address are both set to http://mysite.com/blog (without the www)

    Consequently when I enter http://www.mysite.com/blog into my browser, I am redirected to http://mysite.com/blog. The same applies to a random page on my site, e.g. http://www.mysite.com/blog/about redirects to http://mysite.com/blog/about.

    Without the MemberAccess plugin activated, everything works as you would expect. No problems.

    However, when I activate the plugin, set both pages and posts to be protected, with non-members being redirected to the WP login screen, I get a problem.

    If I enter http://mysite.com/blog/ or http://mysite.com/blog/about, then I am asked to login, then am redirected to the appropriate page. All good.

    Yet, if I enter http://www.mysite.com/blog/ or http://www.mysite.com/blog/about, I am also asked to login, but am then redirected to http://mysite.com/blog/wp_admin, i.e. I am not redirected to the page I wanted to see, rather always end up at the dashboard page.

    What I would like is to enter http://www.mysite.com/blog/about, have MemberAccess ask me to login, then be redirected to http://mysite.com/blog/about, and not the dashboard.

    Could anyone give me any tips on how to sort this out?

    http://wordpress.org/extend/plugins/member-access/

  2. jackzelig
    Member
    Posted 1 year ago #

    After spending all day on this, I've solved my own problem.
    Here's what I did, in case it helps someone else:

    It seems that when the plugin catches a non-logged in user trying to view protected content, it creates a $redirect_to variable, which it then passes to wp-login.php.

    $redirect_to = urlencode($proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

    The problem is, that if I enter 'http://www.mysite.com/blog', $_SERVER['HTTP_HOST'] will hold 'www.mysite.com'. If site address is set to 'http://mysite.com' in my WP backend however, this will confuse the login script and the user will end up at the dashboard (presumably by default).

    To sort this out, one could hard code the site url into the $redirect_to variable, although this is a bit ugly.

    Alternatively, one could check if site_url() starts with 'www.' and if it doesn't, remove it from $_SERVER['HTTP_HOST']

    Something like this:

    if (preg_match('/^www./', site_url())){
      $host= $_SERVER['HTTP_HOST'];
    }else{
      $host = preg_replace('/www./', '', $_SERVER['HTTP_HOST'], 1);
    }
    $redirect_to = urlencode($proto . $host . $_SERVER['REQUEST_URI']);
  3. jackzelig
    Member
    Posted 1 year ago #

    Oops, I forgot to mask the dot after the www in the reg exp.
    It should be:
    $host = preg_replace('/www\./', '', $_SERVER['HTTP_HOST'], 1);

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.