1

In WordPress I have a post with URL https://www.example.com/article.html.

I renamed its permalink to https://www.example.com/article-new.html.

Now if I open the old URL https://www.example.com/article.html then it auto 301 redirects to https://www.example.com/article-new.html due to the WordPress built-in feature by default.

However, I want to show a 404 when someone tries to open https://www.example.com/article.html.

I tried putting this in .htaccess but it gives a 500 server error instead:

RewriteEngine On
Redirect 404 /article.html

Please advise.

1

I think doing this within WordPress is probably the preferred approach, as mentioned in @Harrison's answer, however, to answer your specific queries...

RewriteEngine On
Redirect 404 /article.html

Aside: The RewriteEngine directive relates to mod_rewrite, however, Redirect is a mod_alias directive - so these two directives are unrelated.

However, this should "work" with a default WordPress front-controller, so maybe you have a conflict with other directives in your .htaccess file?

Try the following instead, using mod_rewrite at the top of your .htaccess file (before any existing WordPress directives):

RewriteRule ^article\.html$ - [R=404]

If this still results in an error, then try resetting the 404 error document (to the Apache default) before this:

ErrorDocument 404 default

You'll need to clear your browser cache to clear the cached (permanent) 301 redirect to the new URL.

| improve this answer | |
  • Thanks...first I tried RewriteRule ^article\.html$ - [R=404] but it din't worked so I tried ErrorDocument 404 default RewriteRule ^article\.html$ - [R=404] and it worked..but the 404 page is not the default wordpress page..its just white page with 404 error. is there a way to display 404 wordpress page? – samjoezzy May 19 at 17:03
  • The initial 500 error you were getting suggests there was a "problem" with the previously defined Apache 404 custom ErrorDocument. What (if anything) was this set to previously? However, you wouldn't necessarily expect to be able to trigger the WordPress 404 page from .htaccess directly (which is essentially outside of WordPress itself). Does your theme have a 404.php file? This might need modifying to make it work in this way? (Which is why it might be preferable to solve this entirely within WordPress instead?) – MrWhite May 19 at 17:38
1

I don't know about the right way to change htaccess, but you could try manually redirecting the old url to your 404 template. Adapted from this post:

Use a template file for a specific url without creating a page

Using your example URL slug:

function wpse_manual_redirect( ){
    $str = 'article.html';
    $url = parse_url( add_query_arg( array() ), PHP_URL_PATH );
    $url_length = strlen($url);
    $last_segment = substr( $url , $url_length - strlen( $str ) );

    if ( $last_segment === $str ){
        $load = locate_template( '404.php', true );
        if ( $load ){
            exit();
        }

    }


}

add_action( 'init', 'wpse_manual_redirect' );

Include in functions.php.

| improve this answer | |
  • Hi, Thanks. I tried it. URL redirects to "page not found" but when I check redirect status for that old url via redirect-checker.org it shows 200 OK. I want the status to be 404. Please advice. – samjoezzy May 19 at 16:29

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.