简体   繁体   中英

How to return a 404 for nonexistent WordPress http pages rather than 301 and then 404

Someone has created thousands of backlinks to non-existing pages of my website. Unfortunately, Googlebot tried to crawl these pages. My website is in HTTPs. Googlebot hits the HTTP version of the page, my server first returns a 301 redirect and then, it returns a 404 at the second Googlebot query. It slows down crawling a lot. In fact, right now, Googlebot sees the 301 and decides not to follow the redirection right away.

I wonder if it would be possible to return a 404 before returning a 301 maybe by adding a directive in the Htaccess file. Meanwhile, I would like the server to return a 301 redirect to HTTPS when Google tried to visit the HTTP version of an existent WordPress page.

Example: So, HTTP://example.com/jdfjdfd/ does not exist. Googlebot visits it and returns a 301, and then my server returns a 404 when Googlebot visits HTTPS://example.com/jdfjdfd/ I would like that my server returned a 404 error page when Googlebot visits HTTP://example.com/jdfjdfd/

I tried to add the redirection directive after the WordPress directive but it does not work:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} off
rewriterule ^(.*)$ https://example.com/$1 [R=301,L]

but it does not work.

maybe it can only be done in PHP with something similar to this with a modification in functions.php?

// add_action('template_redirect', 'redirect_core', 5000);
// add_action('init', 'redirect_core', 5000);
// add_action('wp_loaded', 'redirect_core', 5000);
function redirect_core(){
  if (!is_ssl() ) {
    wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);
    exit();
  }
}

Based on your shown samples, could you please try following. Please clear browser cache before testing your URLs.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{HTTPS} off
Rewriterule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Keeping rule for applying https at top is always recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM