Rewrite "old" shortlink URL's


WordPress has a nasty behavior to add shortlink URL’s into your source code like

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/*.conf.pre;
        try_files $uri $uri/ /index.php?$query_string;
        include cleavr-conf/*;

This works so far for the URL I mentioned above, but it also works for

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 /.


Thanks, I wil try this