Forum Moderators: phranque
(
if (a) ||
if (b) ||
if (c)
) &&
d (
RewriteCond %{REQUEST_URI} /phpMyAdmin/ [NC,OR]
RewriteCond %{REQUEST_URI} /cpanel/ [NC,OR]
RewriteCond %{REQUEST_URI} /frontend/ [NC,OR]
RewriteCond %{SERVER_PORT} ^2083$ [OR]
RewriteCond %{REQUEST_URI} /whm/ [NC,OR]
RewriteCond %{REQUEST_URI} /configserver/ [NC,OR]
RewriteCond %{SERVER_PORT} ^2087$
)
RewriteCond %{REMOTE_ADDR} !^123.45.
RewriteRule ^ - [F]
# if requested uri path begins with one of these directories
RewriteCond %{REQUEST_URI} ^/(phpMyAdmin|cpanel|frontend|whm|configserver)/ [NC,OR]
# or if requested server port is one of these
RewriteCond %{SERVER_PORT} ^208[37]$
# and if request is not from an IP that begins with...
RewriteCond %{REMOTE_ADDR} !^123\.45\.
# send a 403 response
RewriteRule ^ - [F]
Whenever possible, put the REQUEST_URI into the pattern of the rule, so the server doesn't have to evaluate conditions on every request ever.
RewriteCond %{SERVER_PORT} ^208[37]$
RewriteCond %{REMOTE_ADDR} !^123\.45\.
#Note, I can't use ^ because "phpMyAdmin" and "frontend" are third level
RewriteRule (?:phpMyAdmin|cpanel|frontend|whm|configserver)/ - [NC,F] RewriteCond %{REMOTE_ADDR} ^123\.45\.
RewriteRule ^ - [L]located in the same area where you give blanket access to robots.txt and your error documents (i.e. before all other RewriteRules, rather than at the end where you'd normally put [L] rules). This of course depends on whether there are other rules, such as internal rewrites, that have to apply to you too. RewriteRule ^(phpMyAdmin|cpanel|frontend|whm|configserver)/ - [F]--after which all that remains is checking for the server port. But if they went to example.com:2083 then it wouldn't match.It doesn't need to, because this pattern of requests is already covered by the part of your canonicalization redirect that looks at HTTP_HOST; they'll be forcibly redirected whether they like it or not.
I'd still be inclined to make one rule just for the files:
# send a 403 response if requested uri path contains one of these directory names and if request is not from an IP that begins with...
RewriteCond %{REMOTE_ADDR} !^123\.45\.
RewriteRule /(phpMyAdmin|cpanel|frontend|whm|configserver)/ - [F] # if requested server port is one of these
RewriteCond %{SERVER_PORT} ^208[37]$
# and if request is not from an IP that begins with this
RewriteCond %{REMOTE_ADDR} !^123\.45\.
# send a 403 response
RewriteRule ^ - [F] this pattern of requests is already covered by the part of your canonicalization redirect that looks at HTTP_HOST; they'll be forcibly redirected whether they like it or not.