Try using the pre_handle_404 hook instead:
add_filter( 'pre_handle_404', 'wpse_383506', 10, 2 );
function wpse_383506( $preempt, $wp_query ) {
if ( ! empty( $_GET['page_id'] ) &&
'342' === $_GET['page_id']
) {
// Throw a 404 error.
$wp_query->set_404();
// And prevent redirect to the page permalink (pretty URL).
remove_action( 'template_redirect', 'redirect_canonical' );
}
return $preempt; // always return it
}
Related Posts:
- How to force a 404 on WordPress
- Redirect Restricted Page to 404
- How to prevent automatic redirection of 404 errors and “incorrect” URLs?
- How do I skip wordpress’s 404 handling and redirect all 404 errors for static files to 404.html?
- How do I programmatically generate a 404?
- How do I skip wordpress’s 404 handling?
- 404 redirection and Google’s index [closed]
- Redirect feed to 404 page
- Check if post is in draft or pending review?
- Why specific file directory fails to redirect but its parent folder succeeds?
- WordPress blocking/preventing viewing of .cgi file in subdirectory [closed]
- Deny access to a path (give 403 or 404 response)
- Defining a custom 404 for non-WordPress parts of site
- WordPress Auto-Redirecting on 404
- Built-In Postname Redirection vs Hard Redirects in relation to SEO and performance
- How to redirect a wrong search on my wordpress website to the error 404 page?
- WP page 404 error only on mobile
- Force 404 redirect to home?
- How can I redirect users on the new 404 page without plugin?
- How to redirect an old page url to the homepage
- Dynamic 404 page content while still keeping 404 status code?
- Redirect to home if page doesn’t exists [duplicate]
- Redirect to parent page if child does not exist
- How to redirect non existent pages/posts in wordpress using htaccess
- How to Redirect WordPress domain.com/?anyword to 404 Page?
- How do I redirect the browser to 404 page, if no posts are found in home page
- How do i make WordPress return 404 instead of redirecting on misspelled page?
- How to redirect if 404 error in wordpress then redirect 404.php url
- Redirect to /404/
- Page editing in backend redirecting to 404 page not found with url /post.php
- How do I skip wordpress’s 404 handling?
- Subpages return 404 server error
- WordPress subfolders htaccess bypassing the root htaccess RewriteRule
- Why my htacess rewrite return 404?
- How to Redirect Url to homepage?
- Stopping media file redirects
- Forcing header cache control in 404 pages
- redirect to 404 page
- Redirect certain 404 pages to parent page
- Redirect of specific words in URL ahead of website migration
- How to configure nginx to redirect requests to the uploads directory to the production server?
- Facebook like button that redirects?
- WordPress redirect loop on nginx + apache reverse proxy
- URL Redirect and Bulk Actions in wp_list_table
- Redirect too many times htacces switching to https
- How to hide that a certain non-WordPress directory exists on the server?
- Redirect “any page/page/number/” to “any page”
- WordPress multisite htaccess redirection to new domain
- Use htaccess to redirect WordPress to static website in a subfolder
- How to redirect URL with subfolder to the same URL but without subfolder?
- If User is NOT Logged in and Page ID is not 6 – Redirect to Login
- Snippet for own (gr)avatars
- SSL and www to non-www redirection works only on homepage – WordPress
- How to redirect if a background request is still being executed
- WordPress keeps redirecting subpages without http to https homepage after switch
- Redirect non-existent subpages to second part of url
- How to remove the [L] Flag for WordPress index .php mod_rewrite
- ReDirect subfolder link to another sub-folder and force SSL
- Two WordPress installations: One in root and one in sub directory – no images in subdir installation [closed]
- What is the most efficient (ie. less server intensive) way for 301 redirects?
- A WordPress site with more than one domain name / Parked domains
- Logout without confirmation and SAME window on mobile
- Will deleting WP’s code in .htaccess cause problems?
- Migrating to Tumblr [closed]
- Redirect 301 of old urls to wordpress urls
- How can I redirect a request to the site root using htaccess, if there’s not a specific cookie set?
- How to fix automatic redirects?
- redirect “about author” code to about page
- get_bloginfo prints IP instead of url
- How to redirect all page requests to a single “goodbye” homepage? [duplicate]
- redirect second domain to static page
- Endless Redirects Suddenly Disable Site
- Redirecting home to /wp-login.php?action=register
- How to redirect a child page to its parent page?
- Redirect to custom login page if not logged in and redirect to home if logged in user try to login, how?
- Erase the HTML website and replace it with a WordPress website -301 Redirection
- How to replace the links http to https
- Redirect user after login to prior page through a wp_login hook
- Auto redirect to different web page
- Redirect only PDFs in different folders?
- Login redirects to home page and doesn’t log in
- using wp_rewrite but keep the original url in the address bar
- Some links keeps redirecting to another domain
- Redirect user on login using extra field value on login form
- Cannot get 301 redirection in htaccess to work (either Redirect or Rewrite)
- Redirect every link that starts with
- I have cloudflare flexible enabled, How to redirect to https for Nginx server
- Redirecting page 1 to default permalink
- Redirect problem: How to redirect to the original URL after login
- Login Form Redirection
- Wrong Redirection
- Rewrite rule for incoming urls
- Hosting the WordPress website on someone else’s web URL
- WordPress Logout Redirect Follow up
- Facebook redirect buton
- How do make WordPress to let alone my static home page?
- Simple function not working on frontend and can’t figure a way to debug it
- Why when I click on a link into the WP admin panel I am redirected to a link that doesn’t contains wp-admin prefix, so I obtain 404 error
- Site1 wordpress website redirect to other site2 in my same server
- Regex and config for redirecting all URLs, except /wp-admin and /specificpage, to a specific page within the same domain, using the Redirection plugin