0

Is there a way I can redirect the URL slug of the 404 template to /404/ so so for example if I have /category/media/blah it redirects to /404/

Is that possible I don't know if it can be done using .htaccess.

Ronny

  • A redirect is a 30x status code, a 404, as the number says, is a 404 code. :) The moment you redirect a request, it's not a 404 anymore. – fuxia Jul 13 at 12:30
  • Note there are big negative consequences to doing things this way. For example if google sees a page returns a 404 it'll update its index, but you want a redirect which is a 301. On top of that, your page titled /404 will return a 200 code, not a 404 code. Have you considered using the 404.php template that WP automatically loads on 404's instead? You don't need a page template and a page to style 404 errors – Tom J Nowell Jul 13 at 14:06
0

So you can do something like this:

  1. Open the 404.php file and add these lines to the top of it. If you don't have one, create it. This will ensure WordPress uses this file for all permalinks that no longer exist or never existed.
    <?php 
    /**
    * Template Name: 404 Page
    */
    
    $four_oh_four = get_permalink( get_page_by_path( '404' ) );
    wp_redirect( $four_oh_four );
    exit();
  1. Create a page with slug '404'.The above code will now redirect from the regular 404 template to a page with '404' slug.

I haven't tested it but should work.

Optionally, you could also use PHP's default redirection using headers if you want to. I just used WordPress' redirection API.

| improve this answer | |
  • 2
    Do consider what @fuxia says above. You'll have problems with your SEO if you do what you desire but you have a solution to what you want. – Fahad Jul 13 at 12:44
  • Actually thinking about Im not going to change the url slug as its not needed – Rejaur Rahman Jul 16 at 2:36
  • Yeah, in most cases, you won't need a redirect. Just utilize the 404.php in the best way possible. You can link to other posts on the 404 pages. – Fahad Jul 16 at 3:39

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.