• Resolved peter8nss

    (@peter8nss)


    When Content Control does a redirect to login as a result of a restriction applying it appears to strip the trailing slash from the url. As a result it causes a 301 redirection later on. In the apache log I can see:

    • “GET /event/test-y6/ HTTP/1.1” 302 – request being redirected by CC
    • “GET /wp-login.php?redirect_to=http%3A%2F%2Flocalhost%3A8080%2Fevent%2Ftest-y6 HTTP/1.1” 200 – sent to login page passing the redirect information (but missing trailing slash)
    • “GET /event/test-y6 HTTP/1.1” 301 – request not quite complete because of missing trailing slash
    • “GET /event/test-y6/ HTTP/1.1” 200 – WordPress revised url now ok

    Functionally this isn’t a problem, but I’d prefer to avoid unnecessary 301 redirections if possible.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Daniel Iser

    (@danieliser)

    @peter8nss – I feel like I need to just add you to team slack ??.

    The issue clearly lies here: https://github.com/code-atlantic/content-control/blob/c1a28bee7407a8d6332c9cd652654a2115f972a6/inc/functions/content.php#L119

    I’ll have to come up with a more reliable way to get the url there.

    Good catch.

    Plugin Author Daniel Iser

    (@danieliser)

    @peter8nss – You might try the following:

    function get_current_page_url() {
        global $wp;
        
        // Get base URL without the query string.
        $base_url = home_url( $wp->request );
        
        // Check if it has a trailing slash, if not add one, unless there's a query string.
        if ( empty($_SERVER['QUERY_STRING']) && substr($base_url, -1) !== '/' ) {
            $base_url .= '/';
        }
    
        return add_query_arg( $_SERVER['QUERY_STRING'], '', $base_url );
    }

    Or this one

    function get_current_page_url() {
        global $wp;
        
        // Get base URL without the query string.
        $base_url = home_url( $wp->request );
    
        return add_query_arg( $_SERVER['QUERY_STRING'], '', trailingslashit( $base_url ) );
    }
    • This reply was modified 1 year, 5 months ago by Daniel Iser.
    Thread Starter peter8nss

    (@peter8nss)

    Tested a variant of the second one which seems to work.

    function get_current_page_url() {

        global $wp;

        /* phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */

        return add_query_arg( $_SERVER[‘QUERY_STRING’], ”, trailingslashit( home_url( $wp->request )  );

        /* phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */

    }

    Plugin Author Daniel Iser

    (@danieliser)

    @peter8nss – only difference being yours is condensed without extra var declaration correct? Just want to make sure, nothing else looked different.

    Will get that patched this week.

    Thread Starter peter8nss

    (@peter8nss)

    I think so. I came up with that before I’d fully read your response as it seemed to minimise the code change.

    Thanks for fixing this.

    Plugin Author Daniel Iser

    (@danieliser)

    @peter8nss – Not a problem. Appreciate the reports. With each one the plugin gets more and more reliable, so its all good.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Redirect strips trailing slash’ is closed to new replies.