Trailing Slashes for PDF Documents

#12085
Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    I can suggest that you add the following filter in your theme’s functions.php file to remove the trailing slash:

    add_filter( 'attachment_link', 'modify_local_attachment_links', 10, 2 );
    function modify_local_attachment_links( $link, $id )
    {
        if(isset($link)){
        $link = unstrailingslashit($link); 
        }
        return $link;
    }

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Hi Michael,

    thanks for your reply. I tried to add this code to my functions.php file, but it didn’t change the behaviour of RankMath – the trailing slash is still there.

    Any ideas why this could be?

    – Martin

    Hi Martin,

    Apologies for the delay.

    I have raised this issue with our dev team and we will be getting back to you in a short while.

    We appreciate your patience. Thank you.

    I upvote for a solution for this!

    The problem is in the file
    wp-content/plugins/seo-by-rank-math/includes/modules/redirections/class-redirector.php
    within the function redirect() at line 145:

    $this->redirect_to = trailingslashit( $this->redirect_to );

    This line adds the trailing slash; if removed it works fine again.

    Until fixed by the RankMath developers the following filter will fix it:

    
    add_filter('wp_redirect', function($location) {
    	if(strpos($location, get_site_url()) === 0) return $location;
    	return rtrim($location, '/');
    });
    

    Also adding the query string (line 147) is not smart enough and will fail, if the target already contains a query string. In this case & should be used and not ? when adding the query string…

    • This reply was modified 5 years, 4 months ago by Michael Baierl. Reason: No trimming for local URLs

    Thanks Michael. I’ve added your code to my functions.php, but the problem persists.

    Hi Metoki,
    that’s right, in your case (local PDFs) the fix should to be something like

    
    add_filter('wp_redirect', function($location) {
    	if(preg_match('/\.pdf$/', $location)) {
    		return rtrim($location, '/');
    	}
    	return $location
    });
    

    In the end it’s just a hack that will modify ALL WordPress redirects. Rank Math has to properly fix this on their end – since there are no filters that’s the only way to do it for now…

    Hello,

    I have forwarded this issue to our Development team, they will further debug this and fix it in the upcoming updates.

    We appreciate your patience. Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)

The ticket ‘Trailing Slashes for PDF Documents’ is closed to new replies.