Rewrite "old" shortlink URL's

Hi,

WordPress has a nasty behavior to add shortlink URL’s into your source code like https://domain.com/?p=111

If the post with this ID exists, WP wil redirect the request to the right post. If you remove a post, the URL will end into a 404 error. Today I got a list of 10 via the Google Search Console and I want to get rid of them. Years ago I added this hook to my theme:

    remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );

But this works only to hide them.
To remove them from GSC you need to redirect them. This is what I did:

location / {
        if ($query_string ~ "^p=([0-9]*)$") {
          return 410;
        }
        include cleavr-conf/domain.com/*.conf.pre;
        try_files $uri $uri/ /index.php?$query_string;
        include cleavr-conf/domain.com/*.conf.post;
    }

This works so far for the URL I mentioned above, but it also works for
https://domain.com/some-directory/?p=111

Which is strange because I added this rule into “/” location block.
How to prevent the rewrite other URL’s than “root” type?

As far as I know the location / block will match any request that starts with / which will include /some-directory/?p=111 .

To prevent the rewrite from affecting other URLs than just the root type, you can use: location = / instead of the location /.

2 Likes

Thanks, I wil try this