Changing some redirects

Hi!

I had to restore my site using an old version, and I really don't remember how I've set up the redirects now. What I'm trying to do is to get what's being shown for logged in users at https://www.wheelofcommerce.com to be displayed at https://www.wheelofcommerce.com/all-stores, and what's currently being displayed at https://www.wheelofcommerce.com/coming-soon-page to be displayed at https://www.wheelofcommerce.com.

I've sent my Wordpress credentials with Predrag's name on it through the contact form on Feb 28, you can also find in my last reply to that string of emails my FTP credentials.

Thanks.

  • Vaughan

    Hi Gabriel,

    Hope you're well?

    Can you check the page. https://www.wheelofcommerce.com/all-stores

    See if that's ok?

    I created a new page, then selected the full-width template.

    After some searching I found that you are using page builder template (probably in a custom template for the homepage. So I used that shortcode for that template in the all-stores page.

    With regards to the coming soon page.

    I simply selected that page as the Front Page in your settings options.

    Dashboard > Settings > Reading

    Hope this helps

  • Wheel of Commerce

    Hi!

    First of all, thanks for answering. That page is still accessible for logged out users though. I really don't remember how I did it, but I remember Tyler Postle helped me with it in a chat session. It was like the content of those pages were simply transferred to another URL, that's all -as far as I can remember, no extra pages were created.

    The homepage is giving a redirect error too: "ERR_TOO_MANY_REDIRECTS". I have a "feeling" this might have something to do with one of my mu plugins, logged-in-home-redirect:

    <?php
    add_action('wp_head','wpmy_redirect_logged_in_users_away_from_home');
    function wpmy_redirect_logged_in_users_away_from_home() {
        if( is_user_logged_in() && ( is_page('393') || is_front_page() ) ) {
    		wp_redirect('https://www.wheelofcommerce.com/all-stores');
    		exit;
        }
    }

    Thanks again.

  • Vaughan

    Hi,

    I think this is down to the wp 4.7 version. the redirect just doesn't seem to work any longer with wp_head.

    Can you try changing to:

    <?php
    add_action('template_include','wpmy_redirect_logged_in_users_away_from_home');
    function wpmy_redirect_logged_in_users_away_from_home() {
        if( is_user_logged_in() && ( is_page('393') || is_front_page() ) ) {
    		wp_redirect('https://www.wheelofcommerce.com/all-stores');
    		exit;
        }
    }

    Hopefully, that should sort it.

  • Wheel of Commerce

    Hi. Still getting the same error. Might have something to do with the fact that the homepage keeps redirecting to /coming-soon-page. Maybe, if possible, ask Tyler to take a look? Like I said, he figured that out the last time I tried to do the same thing.

    If you, or anybody else for that matter, can figure this one out, I'm obviously fine with it too; I'm just mentioning Tyler because he's already looked into the same issue before.

    Thanks again, man.

  • Predrag Dubajic

    Hi Gabriel,

    That mu-plugin is specific for logged in users and is used to redirect them from home page to https://www.wheelofcommerce.com/all-stores/

    Visitors are currently being redirected to https://www.wheelofcommerce.com/coming-soon-page/ but I'm not sure where you want it to go?
    What should happen when visitor comes to your site, should it go to https://www.wheelofcommerce.com/coming-soon-page/, https://www.wheelofcommerce.com/ or https://www.wheelofcommerce.com/all-stores/ ?

    Please advise.

    Best regards,
    Predrag

  • Wheel of Commerce

    Hi, and thanks for the reply.

    If you log into the site with the credentials I've provided, you'll see the homepage displaying what I want to be displayed for logged out users as well. That's what they should see at the homepage, with no redirects, so it should go to https://www.wheelofcommerce.com/ .

    The page https://www.wheelofcommerce.com/all-stores/ is already displaying what's supposed to display, the problem being that it's currently accessible to logged out users -which it wasn't before.

    Thanks again.

  • Adam Czajczyk

    Hello Gabe!

    I tried to find credentials that would let me login to the site and check it but the only recent WP login and password that I found is from January (was sent to Vaughan) and doesn't work. I found another message that you sent to Predrag but that only contains FTP details so doesn't give an access to your site.

    I might be missing something but it would be great if you could re-send admin access credentials to me or Predrag.

    Thanks!
    Adam

  • Wheel of Commerce

    Hi!

    It has something to do with the plugin Page Restrict. It seems to be working erratically. You can find its settings at https://www.wheelofcommerce.com/wp-admin/options-general.php?page=pagerestrict. You can also find a Restriction panel when you go to the edit area for every page, right beneath the edit box. I've checked again the Restriction panel there for some of the pages I want restricted to logged in users and the restriction was on, so they shouldn't be available for logged out users, yet they are.

    I've also found out that whenever a page that has restriction on is updated (I mean by clicking the update button on that page's edit area), most pages, including the homepage, start redirecting to /coming-soon-page. I vaguely remember Tyler helping me set up a line somewhere to do just that, then helping me again when I had issues turning it back to the way it was, so it might be a good idea to talk to him if you can. I've checked my .htaccess and my child theme's functions.php, but didn't find anything that, in my view, could be causing this. But then again, I'm obviously no expert, so if you can, I ask that you take a look at them -all credentials were sent with your name on the subject line yesterday.

    To put it simple: I want all pages to be blocked for logged out users, except for the homepage, contact us, conditions of use and login page. It used to be like that, I have no idea what caused this odd behavior, since this backup I've restored the site to was working just fine. Right now, all pages are accessible to logged out users.

    Thanks.

  • Predrag Dubajic

    Hi Gabriel,

    I'll check with Tyler if he can remember the changes he was making back then but in the meantime you could try our adding this code in child theme functions.php or as mu-plugin:
    add_action('template_include','wpmy_redirect_logged_out_users_to_home');

    function wpmy_redirect_logged_out_users_to_home() {
        if( !is_user_logged_in() && ( !is_page('530') || !is_front_page() || !is_page('586') || !is_page('324')) ) {
    		wp_redirect('https://www.wheelofcommerce.com/');
    		exit;
        }
    }

    With that code if user is not logged in all pages except for the homepage, contact us, conditions of use and login page should redirect back to home.

    Best regards,
    Predrag

  • Wheel of Commerce

    Hi, Predrag!

    Thanks for that. The issue now is that when I have that code in the site, while logged in as super admin, all pages give me a 505 error, and while I'm logged out, I can access the pages listed in the code, but the rest gives me a 505 error, as opposed to just redirecting to the homepage. I'm using the code as a mu plugin.

    The plugin Page Restrict should already do that, I don't know why it stopped working. What's even weirder: I get 505 errors when I deactivate Page Restrict too.

  • James Morris

    Hello @Wheel+of Commerce,

    Doing some preliminary tests that wouldn't interfere with your other devs, I'm finding that when this mu-plugin is activated, you're getting too many redirects.

    Looking at your Domain Mapping settings, I see you are forcing https for admin and front-end pages. I checked your primary domain's setup under Network Admin -> Sites -> {Primary Domain} -> Settings and I'm seeing that, in the database, the site still uses the http schema. I then checked and see that you are using SSL Insecure Content Fixer to perform your redirection to the https schema.

    I believe this is where the conflict is, but without being able to disable this plugin at the moment, I'm unable to test this for sure.

    Once your other devs are done performing what they need to do, please reply back here so one of us can test this further.

    Best regards,

    James Morris

  • Adam Czajczyk

    Hello Gabriel!

    I visited the page (not logging in though) and it looks like now something else is going wrong. The site doesn't seem to load for me completely (tried that in both Firefox and Chrome) and some resources - mostly styles - are not applied.

    The error that I'm getting in Chrome's console says that some resources were "Resource interpreted as Stylesheet but transferred with MIME type text/html: ". I'm sure that was not happening when I visited your site recently so some things must have changed.

    Also, that only happens when I try to access the homepage but not if I'm visiting e.g. "Laptops" store category (which by the way doesn't redirect me anywhere while I'm not logged in user).

    Can you tell me please what has been changed on site apart from adding code suggested by Predrag (and if it's still applied)?

    Best regards,
    Adam

  • Wheel of Commerce

    Hi, Adam! Thanks for replying so fast.

    You're right. This happened after James Morris (the guy who replied my previous post in this thread) went there to fix the problem. He mentioned issues regarding redirects in his reply here. The odd thing is that I'm still seeing my homepage all messed up even after deleting the mu plugin he added -which I'm posting below.

    <?php
    /*function wpmudev_page_template_redirect()
    {
        if( !is_user_logged_in() && ( !is_page('530') || !is_front_page() || !is_page('586') || !is_page('324')) )
    	//if( !is_user_logged_in() )
        {
            wp_redirect('https://www.wheelofcommerce.com/');
    		//wp_redirect( home_url() );
            exit();
        }
    }
    add_action( 'template_redirect', 'wpmudev_page_template_redirect', 10, 3 );
    */

    Something to take into account is that I can see the homepage normally when logged in.

    Given the fact that this particular issue started after James went there, if you can talk to him, that would be great. If you want to access the site, like mentioned before, all the necessary credentials were sent to you guys through email on April 4th, with Predrag's name on the subject line.

    Thanks again.

  • Adam Czajczyk

    Hey Gabe!

    Thank you for your response. I accessed the site and tried to check it but I ended up contacting James. By reading his recent post here I'm not sure if he made any changes after all or was only about to make them so I think it'd be better if I asked him. He's currently offline so I'm awaiting his response and once he only talks back to me one of us will get back to your site and we'll update this thread again for you.

    Best regards,
    Adam

  • James Morris

    Hello @Wheel+of Commerce,

    Just to give you a quick update. Any changes I made to the site during testing were reverted back to where they were prior to accessing the site. However, I will be continuing to work on this today. If possible, please don't let anyone else work on the site today so that I can ensure we're not working over top of each other.

    Thank you for your patience while we sort through this issue.

    Best regards,

    James Morris

  • James Morris

    Hello @Wheel+of Commerce,

    Taking a look at the Page Restrict plugin you had installed, it was a very old plugin that was not tested with WordPress 4.x. It was behaving very erratically. I looked at what pages you were restricting access to using that plugin and it seems you were only restricting access to /register, /all-stores, and /groups. The simplest and most reliable way to restrict access to these pages was to rewrite the mu-plugin to only restrict access to these pages.

    Here's the code I've placed in wp-content/mu-plugins/conditional-redirect.php

    <?php
    function redirect_loggedin_users() {
    if ( ! is_user_logged_in() && is_page(array(882, 6, 326))) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_loggedin_users' );

    After some testing, this seems to do the trick nicely and there are no more redirect loops. Whenever anyone who is not logged in tries to access the pages mentioned above, they are redirected to the homepage.

    Is this the behavior you were wanting?

    Also, after disabling the Page Restrict plugin, you were having some fatal errors related to lines 96-116 in your functions.php. This was causing a 500 Internal Server Error. I've commented out these lines.

    Please let us know if there's anything we've missed and if you need further assistance.

    Best regards,

    James Morris

  • Wheel of Commerce

    Hi, James!

    Thank you very much for that. I've tested and most things are working as intended. /register, however, isn't redirecting logged in users to the homepage. Regarding /all-stores and /groups, logged in users should be able to access them, and it looks like they are -I don't know why that was listed on Page Restrict.

    What I want is to have all pages redirecting logged out users to the homepage, except for /login, /contact and /conditions-of-use. Also, logged in users should get redirected to the homepage when they access /register.

    Regarding that last part, I've noticed something odd: right now, when logged in users access /register, a list of slugs belonging to other users is displayed under "Stores (sites) you're a member of:". I'm presuming this has something to do with having said users under a friends list or something, but I'd rather confirm with you because it does seem weird to me. You can check that by logging into the site using the credentials of the test account I've provided in that same email sent to Predrag on April 4th.

    Thanks once more!

  • James Morris

    Hello Gabe!

    OK, this took a little updating, but I believe I've got the base code you'll need figured out. You may need to fine tune this a little bit if you find any other pages that do not redirect properly.

    The problem I ran into is you use some plugins that use "virtual pages" these pages have to have special treatment. But, the following code accommodates for this and performs the needed redirects without looping. Here's what I added to the mu-plugin.

    // Redirect logged in users away from /register to homepage
    function redirect_loggedin_users() {
    if ( is_user_logged_in() && is_page('326')) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_loggedin_users' );
    
    // Redirect visitors to homepage except on defined pages
    // Special consideration in the first is_page array given for virtual pages
    function redirect_visitors() {
    if ( !is_user_logged_in() && is_page(array('all-stores', 'groups', 'customer-reviews')) || !is_user_logged_in() && !is_page(array('contact', 'conditions-of-use', 'register')) && !home_url() && !site_url('/login') ) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_visitors' );

    Now, if you need to add anymore pages that need to be forced to redirect to the homepage for visitors, all you have to do is add them to the following is_page() array. You can use the slug or page ID#.

    is_page(array('all-stores', 'groups', 'customer-reviews'))

    If you want to add pages that you want to exclude from redirection for visitors, you would add them to the following !is_page() array (is not page).

    !is_page(array('contact', 'conditions-of-use', 'register'))

    I've worked with another staff member to test all this out and everything seems to be working correctly on the URLs defined. If there are any other pages not redirecting that you need redirected, it's because they are likely virtual pages and need added to the above array.

    I hope this clarifies a bit.

    Best regards,

    James Morris

  • Wheel of Commerce

    Hi again, James!

    Thank you very much for looking into it for me, I know my site is more complicated than the average.

    /register is still showing for logged out users even though I've added it there. For logged out users, wouldn't it be easier to just list the pages I want them to have access to? Because those are only the homepage, /contact and /conditions-of-use, everything else should be redirecting them to the homepage.

    Regarding those subsites listed at /register when you access it with a test account: what are they?

    Thanks.

  • James Morris

    Hello Gabe,

    The reason the /register page is still showing is because of the second part of the conditions. It's being listed twice. See:

    if ( !is_user_logged_in() && is_page(array('register', 'all-stores', 'groups', 'customer-reviews')) || !is_user_logged_in() && !is_page(array('contact', 'conditions-of-use', 'register')) && !home_url() && !site_url('/login') )

    So, basically, by having /register in both is_page and !is_page it was cancelling it out. I've removed it from the !is_page. So, now logged in users and logged out users redirect to homepage when this URL is accessed. However, this essentially blocks this page from being accessed entirely.

    In your previous post, you said...

    /register, however, isn't redirecting logged in users to the homepage.

    And in this latest reply, you said...

    /register is still showing for logged out users even though I've added it there.

    Wouldn't you want /register to show only for logged out users? That's the behavior I had setup the plugin to do originally.

    If you only want the page to display for logged out users, you need to change the code to the following:

    <?php
    // Redirect logged in users away from /register to homepage
    function redirect_loggedin_users() {
    if ( is_user_logged_in() && is_page('register')) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_loggedin_users' );
    
    // Redirect visitors to homepage except on defined pages
    // Special consideration in the first is_page array given for virtual pages
    function redirect_visitors() {
    if ( !is_user_logged_in() && is_page(array('all-stores', 'groups', 'customer-reviews')) || !is_user_logged_in() && !is_page(array('contact', 'conditions-of-use', 'register')) && !home_url() && !site_url('/login') ) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_visitors' );

    For logged out users, wouldn't it be easier to just list the pages I want them to have access to?

    Not in your situation. You have some special considerations you have to work around.

    1. You have to ensure that you are excluding your homepage so you don't have a redirect loop.
    2. You have to exclude your login page.
    3. You have to explicitly define your virtual pages as they don't like to play nice with a global match.

    Hence, why there are so many conditions. This was the only way I could get this to work without redirect loops or pages not being blocked. This also may explain why the other plugin was not working reliably.

    Regarding those subsites listed at /register when you access it with a test account: what are they?

    The way the plugin is coded, if implemented correctly, logged in users cannot see this page at all, ever. So this should not be an issue. However, that list is all the sites associated with your email account/user account. This is a standard part of Multisite. But, if you are blocking this page from being accessed to logged in users, it is a non-issue.

    I hope this clarifies a bit.

    Best regards,

    James Morris

  • Wheel of Commerce

    Hi, James!

    Thanks for all of that. For some reason though, logged out users still can access /register. Please correct me if I'm wrong, but from your following statement, I'm presuming /register should be blocked for both logged out and logged in users, right?

    I've removed it from the !is_page. So, now logged in users and logged out users redirect to homepage when this URL is accessed. However, this essentially blocks this page from being accessed entirely.

    If for whatever reason you have to pick one, I'd rather have it blocked for logged out users, because I haven't launched the site yet, so people aren't supposed to register, and that was happening during the period in which the page was open.

    Thanks once more.

  • James Morris

    Hello @Wheel+of Commerce,

    That's what was confusing me a little bit... With /register being blocked for both, it eliminated access to the page for everyone. However, since you explained that it should be blocked for all until you go live, that makes a little more sense.

    To block the /register page for all users until you go live, use this:

    <?php
    // Redirect logged in users away from /register to homepage
    function redirect_loggedin_users() {
    if ( is_user_logged_in() && is_page('register')) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_loggedin_users' );
    
    // Redirect visitors to homepage except on defined pages
    // Special consideration in the first is_page array given for virtual pages
    function redirect_visitors() {
    if ( !is_user_logged_in() && is_page(array('all-stores', 'groups', 'customer-reviews', 'register')) || !is_user_logged_in() && !is_page(array('contact', 'conditions-of-use')) && !home_url() && !site_url('/login') ) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_visitors' );

    To then enable access to the /register page once you go live, use this code:

    <?php
    // Redirect logged in users away from /register to homepage
    function redirect_loggedin_users() {
    if ( is_user_logged_in() && is_page('register')) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_loggedin_users' );
    
    // Redirect visitors to homepage except on defined pages
    // Special consideration in the first is_page array given for virtual pages
    function redirect_visitors() {
    if ( !is_user_logged_in() && is_page(array('all-stores', 'groups', 'customer-reviews')) || !is_user_logged_in() && !is_page(array('contact', 'conditions-of-use', 'register')) && !home_url() && !site_url('/login') ) {
        wp_redirect( home_url() );
                exit();
        }
    }
    add_action( 'template_redirect', 'redirect_visitors' );

    As you can see, all I did was move , 'register' from is_page to !is_page.

    I hope this clarifies a bit.

    Best regards,

    James Morris

  • Predrag Dubajic

    Hi Gabriel,

    I just tested James' code on my installation and it works fine for redirecting visitors away from registration page.

    It seems like there's something blocking this code from working properly on your end, but I'm afraid that with all the plugins and customizations on your site we're unable to determine what the cause is.

    I would suggest setting up staging environment where you can disable your plugins, including mu-plugins as well, and trying with default WP theme to see what's conflicting with the above code.

    Best regards,
    Predrag

  • James Morris

    Hello @Wheel+of Commerce,

    The code samples I've provided and the instructions given should have accomplished this for you. I tested this on your site and it worked well.

    However, if you are wanting both logged in and logged out members redirected from /register to the homepage, then why not set the page as a draft until you go live? Or, use .htaccess to redirect all requests?

    The reason I suggest this is, it's kinda over-complicating things to use programming to redirect all users away from a page. We know we have found a solution to redirect logged in users away from this page already. This was confirmed.

    As a rule, the more complicated you make things, the more difficult they are to maintain. As little code as possible should be used to accomplish the job at hand.

    Best regards,

    James Morris