Mulli
Mulli

Reputation: 1708

How to match hostname in .htaccess using regular expression?

I am trying to block a bot using .htaccess in my WordPress site.

The host name is: ec2-D1-D2-D3-D4.us-east-2.compute.amazonaws.com

Where D1, D2, D3, D4 are sequnce of 1-3 digits, ie. [0-9]+ will match.

In some cases only D1,D2 & D3 exists.

I am trying:

RewriteCond %{HTTP_REFERER} ^ec2-[0-9]+-[0-9]+-[0-9]+\.us\-east\-2\.compute\.amazonaws\.com$ [OR]
RewriteCond %{HTTP_REFERER} ^ec2\-[0-9]+\-[0-9]+\-[0-9]+\-[0-9]+\.us\-east\-2\.compute\.amazonaws\.com$ 

RewriteRule ^.*$ - [F,L]

It does not seem to work. What am I missing?

Upvotes: 2

Views: 355

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133770

Could you please try following, fair warning haven't tested it, also fixed your regex here and HTTP_REFERER doesn't directly starts from hotname so removed ^ part before your hostname too here(all values are samples/test values provided in question only).

RewriteEngine ON
RewriteCond %{HTTP_REFERER} ec2-([0-9]+-){1,}[0-9]+\.us-east-2\.compute\.amazonaws\.com [NC]
RewriteRule ^ - [F,L]

Upvotes: 4

Related Questions