Nat TaylorBlog, AI, Product Management & Tinkering

WordPress Trailing Slash

Published on .

The addition of the trailing slash in WordPress is handled by redirect_canonical()

I wasted a lot of time with other functions before discovering this while attempting to prevent /llms.txt from redirecting to /llms.txt/

add_filter( 'redirect_canonical', 'custom_redirect_canonical', 10, 2 );
function custom_redirect_canonical( $redirect_url, $requested_url ) {
    if( str_ends_with( $requested_url, '/llms.txt' ) ) {
        return untrailingslashit($redirect_url);
    }

    return $redirect_url;
}

Post Navigation

«
»