大网数据广告活动
网站运营与故障问答

Return 410 Gone status for all .html files except a single one?

· 52CE 我爱测

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 (thr…

问题原文

原帖链接 · GeekRider · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-04-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。