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
- Disable WordPress URL auto complete
- WordPress redirecting connections on port 8080 to 80
- 301 redirect instead of 404 when URL is a prefix of a post or page name?
- Redirect to referring page after logging in
- Correct way to hide pseudo pages from being shown?
- Htaccess redirect from ‘/%postname%.html’ to ‘/%postname%’
- Redirect to post after submission with Gravity Forms? [closed]
- Redirect www to non-www htaccess
- Import WordPress Post Slug with Forward Slash
- Create a php callback/endpoint for an OAuth script
- Query WP data with the WPDB API from outside WordPress
- Too many redirects when using wp super cache [closed]
- header and wp_redirect not working. cannot modify header information warning
- Code to create a redirection after login?
- WordPress Redirecting HTTPS Requests to HTTP
- How can I redirect my website’s homepage to a random blog post?
- wp_redirect does not work
- How to migrate production WordPress to Localhost
- How do I redirect all 404 error url to Subcategory url
- How to redirect a page into file?
- Blog index not going to wordpress folder
- Check Page requested, and redirect
- WordPress on a local machine redirecting to xampp dashboard page [closed]
- Can’t control WordPress Addresses from admin panel
- How to use https on one page only?
- WooCommerce – Redirect to a product after login
- How to redirect just one or two blog posts correctly?
- Force WordPress https redirect before www redirect based on site address url
- Prevent wordpress autoredirect
- Custom form redirect after login
- How to redirect only 404 pages with htaccess in WordPress
- How to redirect the frontend of a WordPress site (only)?
- Redirect Attachment Page to Attachment
- Redirect away from page if user is not admin
- redirect to a child theme based on the town of Internet users in france with the verification of the ip address
- How do I change the home page to the registered user’s profile page
- Conditional Redirect
- Upload Directory question
- 301 Redirect /author/username/ to /profile/username/
- Redirection with a wildcard
- How can I stop redirecting from a search to a taxonomy archive page
- How to return a 410 code for previously published posts now set to draft?
- The / location 301-redirects to itself in a loop
- Redirect user from backend to frontend
- Need to redirect from Apache2 Ubuntu Default Page to home page in wordpress
- Make a treatment before the action of the form
- Is it possible to take a new visitor to the site to a disclaimer page and then return them to their previous page?
- How to 301 redirect sub domain post URL to root domain
- Homepage (domain) that redirects user toward two subdomains based on his/her preferred language
- redirect to another page if user has registered successfully
- Too many redirects error when change of base url
- How to redirect new registrars to a custom registration page instead of WP default registration page?
- WordPress redirect users from certain countries to subpage of language version
- When redirecting all URLs to homepage, what exceptions do I need to make?
- Error 404 wordpress redirecting URL
- Redirect from subdomain
- Remove link before another link
- How to handle expired assets/content when user has direct URL
- Best SEO way to redirect from all “posts” to home url
- Is there a way to redirect people from a page on my .com website to a corresponding page on my .co.uk website if they are in the UK
- How to find out what is causing redirect of static content?