2

I bet there's at least one trivial solution to this problem, but I've yet to figure any of them out.

In a nutshell, what I'm trying to achieve is, informing the search engines that there are no more .html files on the server (through a 410 Gone status) except for the Google Search Console verification file (googlerandomstringofchars.html).

Here's what I added to the root .htaccess file, to no avail

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^googlerandomstringofchars.html$
RewriteRule ^.*\.html$ - [R=410]

I basically was trying to exclude googlerandomstringofchars.html, while returning the 410 status for any other .html file.

The website is WordPress driven, but I made sure to add the my directives at the very beginning of the .htaccess file, so they are evaluated before WP has any chance of interfering and outputting its fancy 404-Not Found page.

It doesn't work as I'd expect it to. For some reason, an attempt of accessing either an .html file that doesn't exist, an existing .html file, or the Google .html file, results in WordPress returning its "fancy" 404 page.

But why? Where's the flaw in my directives?

1

Use:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/googlerandomstringofchars.html$
RewriteRule ^.*\.html$ - [R=410,L]

According to http://httpd.apache.org/docs/current/mod/mod_rewrite.html REQUEST_FILENAME is the full local name of the file. So it would be something like /var/www/html/example.com/googlerandomstringofchars.html You should instead use REQUEST_URI which is the relative path on the webserver starting with a slash. I've both changed your condition to REQUEST_URI and added a slash.

You also need a L on your R=410 so that it is the very last rewrite rule used. Otherwise, other rewrite rules (such as ones for WordPress) will also get executed and may override it. I've added this L into your rule.

You need to make sure that this block goes near the top of the .htaccess file. It has to go before other rewrite blocks, such as the ones for WordPress.

  • Brilliant, that fixed it. Even better, I've also learned something really useful. I'm forever in your debt @StephenOstermiller – GeekRider Apr 6 '18 at 22:28
  • "You also need a L on your R=410" - You don't actually need the L flag when you specify a status code outside of the 3xx range (ie. a non-redirect) - it is implied. From the docs: "...and rewriting is stopped as if the L were used." – MrWhite Apr 7 '18 at 15:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.