Based on this answer I created a function to change the shortlink from domain.tld?p=435 to domain.tld/435 and redirect domain.tld/435 to domain.tld/full-permalink:

function shorter_shortlink() {
  global $post;
  $shortURL = get_bloginfo('url').'/'.$post->ID;
  if(!empty($shortURL)) {
  return $shortURL;
  } else {
  return get_bloginfo('url').'/?p='.$post->ID;
add_filter('pre_get_shortlink', 'shorter_shortlink'); 

add_action( 'wp', function(){
  global $wp;
  if( is_numeric( $wp->request ) && empty( $wp->query_vars['page'] ) )
  wp_redirect( get_permalink( absint( $wp->request ) ), 301 );

While testing it seems that this approach is not the best for the performance. I wonder if there´s a better solution for this matter?

  • 1
    If you want better performance, then don't use shortlinks. Seriously, that's the only answer. Any redirect will be slower than no redirect.
    – Otto
    Sep 27, 2014 at 16:08

1 Answer 1


I fear the answer here is that redirecting will just never come with good performance. Especially doing it via PHP, because then it's handled rather late.

You could think about doing it via apache in particular the .htaccess file, which intercepts way earlier, before PHP/WordPress even come into play. Which will be better, but of course it still will be a redirect. But as @birgire pointed out in his comment, you need the concrete/exact rules, so this isn't very practical.

Another idea, doing some javascript "magic", same comment.

If you ask me it's not worth it, either you redirect or you don't, just decide.

  • Is there an possibility to make this work through wp_rewrite without direct editing of .htaccess?
    – John
    Sep 27, 2014 at 13:52
  • You mean like this answer suggests? @John Sep 27, 2014 at 13:57
  • Yes, but as it´s mentioned there, this doesn´t redirect to the full permalink and just makes the post visible at domain.tld/323. @ialocin
    – John
    Sep 27, 2014 at 14:04
  • Right, redirect and rewrite are just two different approaches and have as such their own benefits and drawbacks. @John Sep 27, 2014 at 15:10
  • 1
    @john I'm not sure the proposed RedirectMatch will give you better performance, since the core canonical redirect is determined within the template_redirect hook, that's fired a little bit later than the wp hook I proposed. That means you would still have to travel a little bit deeper into the PHP script.
    – birgire
    Sep 27, 2014 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.