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

Return 410 gone status code for URLs containing same characters in .htaccess

· 52CE 我爱测

My website was hit by the pharma spam attack which created hundreds of URLs, all of which contain the last 5 characters -1234.pdf . So that Google and other search engines will remove these quicker, I'd like to return these pages…

问题原文

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

My website was hit by the pharma spam attack which created hundreds of URLs, all of which contain the last 5 characters -1234.pdf.

So that Google and other search engines will remove these quicker, I'd like to return these pages with a 410/GONE status code, is there an easier way of doing this in .htaccess other than listing all of the URLs and setting RewriteRules for every single one (a process which would take considerable hours!)

已采纳解答

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

To serve a 410 Gone for all URLs that end in the literal string -1234.pdf then you can use the following mod_rewrite directive at the top of your .htaccess file:

RewriteEngine On

RewriteRule -1234\.pdf$ - [G]

Or to reject any URL that ends in -NNNN.pdf where NNNN is a number of at least 4 digits then you can change the RewriteRule pattern to something like: -\d{4,}\.pdf$.

but the URLs are still returning a 404.

In the case where the server has defined a custom 410 ErrorDocument and is overriding the HTTP response code then set the following at the top of your .htaccess file (make sure you've not defined a custom 410 ErrorDocument yourself later in the .htaccess file or server config):

ErrorDocument 410 default

UPDATE: we also have some other spammy URLs which have the string "stmap_9874100.html" (where the last three digits are always different), can a similar query be written to set these URLs to 410's too?

Yes, you can add another directive similar to before:

RewriteRule stmap_9874\d{3}\.html$ - [G]

Explanation of the RewriteRule pattern (regex / Regular Expression) and directive:

  • stmap_9874 - These characters are matched literally (anywhere in the URL-path).

  • \d is a shorthand character class representing any digit (0-9) and \d{3} is exactly 3 of them.

  • \. matches a literal dot, otherwise, a dot in regex matches any character (except newlines).

  • html - These characters are matched literally.

  • $ is an "anchor" that represents the end of the string. So this string of characters must occur at the end of the URL-path.

  • - (hyphen) - not part of the regex, this is the second argument to the RewriteRule directive. This is just a "placeholder" indicating no substitution. The URL is not rewritten/redirected to anything else.

  • The G flag (short for R=410) results in a 410 Gone response. (The L flag is not required here, as it is implied when specifying a response code out of the 3xx (and 2xx) range.)

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

资料来源与适用范围

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

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

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