0

I have many articles linked from external sites without categories. They end up with a 404 not found. A minor migration was done a long time ago and just noticed the 404s.

One of many examples:

incorrect: wrestleview.com/20060-update-on-joey-mercury-and-his-wwe-status

Correct: wrestleview.com/wwe-news/20060-update-on-joey-mercury-and-his-wwe-status

I want when people click on a link with no category to automatically redirect to the ID and post name. Since there is always a unique ID to each post. Can this be done through htaccess or functions.php? I cannot do a 301 redirect manually, I have over 1000 links affected by this.

Thanks

  • All WP pretty (SEO friendly) links are really 404 handled by the main WP index.php file, so you can't tell htaccess to try to sort this issue out. You will need to use wordpress & PHP for this. – Eduardo Escobar Apr 16 '18 at 20:29
  • In the given URL, is "20060" the ID of the post? – Eduardo Escobar Apr 16 '18 at 20:31
  • Correct, that's the unique ID. – Paulwv Apr 16 '18 at 20:32
  • If you know how many digits are possible in the Post ID - say for example they range from 2 digits to 5 digits, and there are no posts of any post type that start with 2 digits - so you can always be sure that a 2-digit to 5-digit number is in fact a Post ID, you can do a regular expression in .htaccess in the form of a RewriteRule. Basically you want to work out how to say, "if the URL starts with the domain, followed by a slash and X digits, rewrite the URL to the Ugly Permalink" (example.com/?p=X) which will then re-redirect to the full URL. – WebElaine Apr 16 '18 at 20:33
  • (That isn't specific to WP so you may want to ask over on StackOverflow with regex and .htaccess tags.) – WebElaine Apr 16 '18 at 20:34
0

I'd suggest you to forget about htaccess in this case.

In your functions.php file add:

add_action( 'template_redirect', 'check_missing_urls' );
function check_missing_urls() {
    if ( ! is_404() ) {
       return;
    }
    $has_match = preg_match( '/\/?(\d+)/', $_SERVER['REQUEST_URI'], $match );
    if ( ! $has_match ) {
       return;
    }
    $new_url = get_permalink( $match[1] );
    if ( ! $new_url ) {
       return;
    }
    wp_redirect( $new_url, 301 );
    wp_die();
}

Please note that this is an untested code, use it as a very basic and rough idea to accomplish what you need.

Also, i'd suggest you to use 302 instead of 301, during the test period.

  • Hi Eduardo, Just tested it on my dev site and it gave me a blank page with a "Wordpress error" – Paulwv Apr 16 '18 at 21:06
  • Did any redirection take place? What about the non-404 pages, are they working ok? – Eduardo Escobar Apr 16 '18 at 21:09
  • Check it out: seo.wrestleview.com/… – Paulwv Apr 16 '18 at 21:10
  • If you remove the code the error disappears? – Eduardo Escobar Apr 16 '18 at 21:18
  • Take a look here prnt.sc/j65jcs . The missing URL is being detected and the new URL is sent to the browser, however, there is a 500 error conflicting with the redirection. – Eduardo Escobar Apr 16 '18 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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