问题原文
原帖链接 · 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).\dis 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 theRewriteRuledirective. This is just a "placeholder" indicating no substitution. The URL is not rewritten/redirected to anything else.The
Gflag (short forR=410) results in a 410 Gone response. (TheLflag is not required here, as it is implied when specifying a response code out of the 3xx (and 2xx) range.)
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
