Return 410 Gone status for all .html files except a single one?
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, mod-rewrite, 410-gone, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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?
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.
Comments
Post a Comment