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

How can I make a specific series of URLS have "410" Gone status?

· 52CE 我爱测

Server was hacked and my .htaccess file was changed to wildcard redirect (spam). I am seeing a lot of requests like those below even after I've cleaned up my server: example.com/cobwebbing-gurkha1486mskh27556/64b75843rl69s2/ exam…

问题原文

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

Server was hacked and my .htaccess file was changed to wildcard redirect (spam).

I am seeing a lot of requests like those below even after I've cleaned up my server:

example.com/cobwebbing-gurkha1486mskh27556/64b75843rl69s2/
example.com/countercheck-gurkha1486mskh22793/n4a670a2h43m/
example.com/countercheck-gurkha1486mskh19573/36c9on2137rs/

How can I change those URL to 410 Gone? I'd like to redirect them to a page I've made (fail.php) that issues a 410 header.

Here is the current hacked htaccess. I tried to make the end part of the file (?gurkha) to go to fail.php but it doesn't seem to work. It keeps going to a the WordPress 404 page. The WordPress 404 eats up system memory and I'd like a more elegant and efficient way to handle these URLs.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^\/(.*)-(gurkha)([0-9]+)mskh([0-9]+)\/(.*)$ ?gurkha$3=$4&%{QUERY_STRING}[L]
</IfModule>

I'd really like to get these URLS off google. How can I change my .htaccess so they are 410 Gone?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

/cobwebbing-gurkha1486mskh27556/64b75843rl69s2/
/countercheck-gurkha1486mskh22793/n4a670a2h43m/
/countercheck-gurkha1486mskh19573/36c9on2137rs/

To force a URL that contains -gurkha followed by a digit fail early with a system generated 404 (bypassing WordPress), then you can use something like the following at the top of your .htaccess file:

RewriteRule -gurkha\d - [R=404]

Note that this must go before the WordPress front-controller. ie. before the <IfModule mod_rewrite.c> directive. You do not need to repeat the RewriteEngine directive.

If, however, you still see a WordPress generated 404 page then it's possible that you have a custom 404 error document that points to a WordPress document (eg. 404.php). You can reset this to the simple Apache default by including the following in your .htaccess file:

ErrorDocument 404 default

If you want Google to drop these URLs quicker then you can change the R=404 flag to G (gone) to serve a 410 Gone instead. (Although this potentially alerts users/bots/hackers to this - but that probably is not a real concern.) Again, reset the ErrorDocument (as above) if you still see a WordPress page returned.

You can then use the Google Search Console URL removal tool to explicitly remove these URLs from the SERPS.

A 401 Unauthorized response won't necessarily make Google drop the URL. And it will only likely raise further suspicion. (Although, it seems the OP initially referenced "401" in error and actually meant "410".)


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^\/(.*)-(gurkha)([0-9]+)mskh([0-9]+)\/(.*)$ ?gurkha$3=$4&%{QUERY_STRING}[L]
</IfModule>

You should remove that last RewriteRule. As it stands, it will not do anything, but also has several "errors":

  1. In per-directory .htaccess files, the URL-path that the RewriteRule pattern matches against never starts with a slash. So, this RewriteRule would never match anything in this context.
  2. By placing this directive after the front-controller, it will only be seen for URL requests that map to physical files or directories. These URLs probably don't map to real files/directories, so again it would never match.
  3. By placing directive inside the # BEGIN WordPress / # END WordPress block then it will likely get overwritten when WordPress updates.

Other, "less serious" points regarding that last RewriteRule:

  1. There is no space between the CondPattern and the flags (ie. before the [L]). This would potentially result in an invalid rewrite. And potentially cause problems if you later added more directives (because there is no L flag).
  2. No need to backslash escape slashes (eg. \/) in standard Apache regex. (Spaces are used as delimiters and slashes otherwise carry no special meaning.)
  3. Many unnecessary captured groups (parenthesised subpatterns) that are not used.

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

资料来源与适用范围

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

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

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