In WordPress I have a post with URL .html
.
I renamed its permalink to .html
.
Now if I open the old URL .html
then it auto 301 redirects to .html
due to the WordPress built-in feature by default.
However, I want to show a 404 when someone tries to open .html
.
I tried putting this in .htaccess
but it gives a 500 server error instead:
RewriteEngine On
Redirect 404 /article.html
Please advise.
In WordPress I have a post with URL https://www.example/article.html
.
I renamed its permalink to https://www.example/article-new.html
.
Now if I open the old URL https://www.example/article.html
then it auto 301 redirects to https://www.example/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/article.html
.
I tried putting this in .htaccess
but it gives a 500 server error instead:
RewriteEngine On
Redirect 404 /article.html
Please advise.
Share Improve this question edited May 19, 2020 at 16:51 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked May 19, 2020 at 15:16 samjoezzysamjoezzy 214 bronze badges2 Answers
Reset to default 1I 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.
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.