Simply looking to deny access to multiple spammy domains that are back linking to my posts and pages.
What you are proposing here (blocking access based on the HTTP Referer
) is not going to "deny access to multiple spammy domains" the way I think you think it will.
In fact, as the webmaster of the site being linked to, you can't programmatically ...
- Prevent a site from linking to your site.
- Prevent that link from being seen by the search engine bots as a do-follow link. (Search engine bots generally do not send an HTTP
Referer
header as part of the request when they crawl your site... at least Googlebot does not.)
At best, this will simply prevent a real user from following a link from the spammy website to your site. But the spammy website can even work around this and prevent the user-agent sending the Referer
, by employing the Referrer-Policy.
Where did you get this list of spammy domains from? Have you confirmed that these sites are indeed linking back to your site and you are not simply seeing the results of referrer spam in your logs? (The HTTP Referer
header is trivial to fake.)
If these really are spammy backlinks and you think Google is likely to serve a negative action against your site ranking then consider submitting these links to Google's Disavow tool - this is the only way to prevent these links being counted towards your backlink profile (for Google at least). However, for trivial/obvious spam links this is probably not necessary.
Reference:
https://support.google.com/webmasters/answer/2648487?hl=en
To answer your specific questions relating to the syntax of these directives:
What confuses me is that some of the domains have different TLD's, some start with a sub domain, some are HTTP or HTTPS, www or not www, so trying to make sure that the code covers all those variables
You need to match against the specific domain the link is coming from. Create multiple rules to cover multiple variations if you wish (if the spammy site does not canonicalise the hostname) or combine using a regular expression - as in your earlier example:
RewriteCond %{HTTP_REFERER} ^http(s)?://([^.]+\.)*tumblr\.com [NC,OR]
For example, the above condition matches HTTP or HTTPS and any number of subdomains of tumblr.com
, including no subdomain at all. So, any of the following, and more...
http://tumblr.com
https://tumblr.com
http://sub.tumblr.com
https://sub.sub.tumblr.com
The parentheses in ^http(s)?://
are not necessary and should simply be removed. ie. ``^https?://. The parentheses result in
s` being captured in a backreference, which is not being used here.
Found a few examples of this code that have the OR tag after the NC, so assuming this is correct, but that the last line doesn't have the OR.
Yes, that is correct. If you included an OR
flag (not a "tag") on the last condition, then it would unconditionally block every request!
The NC
(nocase
) flag is only to catch bot traffic that might send a mixed/uppercase hostname. All mainstream browsers lowercase the hostname when making the request.
Reference:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
The <IfModule>
wrapper is not required. Unless you are intending to use the same code on multiple servers that might not have mod_rewrite enabled and it is OK for these directives not to function.
Code of this nature is typically used for hotlink protection (not for preventing spammy backlinks). ie. Preventing other sites leaching off your static resources (typically images).