What regular expression will match a domain name without a 3rd level? - regex

What is the most efficient regex that will match these domains, without having to specify any rules to ignore?
Example matches:
domain.com
test.com
example.net
company.org
Example Ignore:
dev.domain.com
m.domain.com
www.domain.com
Any top level domain is possible. Essentially I am trying to make sure the domain doesnt already have a 3rd level.

To match a domain with any TLD use this:
^[^.\s]+\.[^.\s]+$

Related

How to use squid regex acl to exclude a specific subdomain?

I want to block a domain but except a specific subdomain.
For example, I use test.domain.com as my production environment and deny all access from squid.
But I want allow squid user to access test.demo.domain.com which is a testing environment.
my squid.conf:
acl denylist dstdom_regex ^(?!.*(demo)).*domain\.com$
http_access deny denylist
http_access allow all
I know what's wrong with my regex. Squid regex use GNU regex (see serverfault question) and GNU regex can not understand (?!.*(demo)).
So how to exclude a specific subdomain in GNU regex?
First, you should use negative lookbehind (?<!) instead of negative lookahead (?!). Second, I don't think it can work with ^ as it matches from the beginning of the domain. Third, with .*domain.com it will match
the whole domain anyway, so you should use \.domain.com instead.
The correct regex that doesn't match demo anywhere before .domain.com would be
(?<!(.*demo.*))\.domain\.com
Here, the domain must end with .domain.com and must not contain demo anywhere beforehand in order to match.
There's no need for regex in your case!
You said you need to allow only one specific subdomain. So add this subdomain in a ACL and allow access to it.
acl allowed_subdomain dstdomain test.demo.example.com
acl blocked_domain dstdomain .example.com
http_access allow allowed_subdomain
http_access deny blocked_domain
http_access deny all
Explanation:
Squid will read the config file looking for a match and the first one it encounters it ends it's job.

Regex for sub-domain

I have several sub-domains configured to a IIS website. I would like to parse the incoming requests in ARR. I would like to match a specific sub-domain that would capture all these different scenarios
http://abc.example.com
https://abc.example.com
http://abc.example.com/xyz
https://abc.example.com/xyz
http://abc.example.com/xyz?q=123
https://abc.example.com/xyz?q=123
I have tried a few things but they don't seem to work and searches only reveal how to catch sub-domains and not just a sub-domain.
Thanks
Try this:
http(?:s)?:\/\/(abc)\..*

Regular Expression That Will Allow URL Redirect Of All URLs Without A String To Redirect To URL With That String

I have some versioned folders of site files that we are handling through IIS. What I need to do is create a URL Rewrite that will redirect traffic from all requests that don't match the most recent version, TO that most recent version. I'm having a difficult time as RegEx are not my specialty and I have been working on it for the last week.
Here's an example of what I need.
Most recent version:
https://testurl.com/v4/#
Older Versions:
https://testurl.com/#
https://testurl.com/v2/#
https://testurl.com/v3/#
These urls have other routes off of the base as well (ex. https://testurl/v3/#/rout1)
I'm needing a regular expression that will say "Any requested url that is does not contain the /v4/ to REDIRECT to the https://testurl/v4/#
Can someone point me in the right direction?
This regex will capture all domains of the form that you've listed. Capture group 1 will contain the actual route, eg / or /rout1.
/^https\:\/\/testurl\.com(?:\/v\d)?\/#(.*)$/
You can see it illustrated here: http://regexr.com/3fgo8

Regular Expressions: Get subdomain and domain

In nginx, I have a line that states ~^(www\.)(?<sub>.+).(?<domain>.+)$.
How do I make it so I can get the subdomain and domain be separate? Like subdomain.example.com.
EDIT:
I tried ~^(www\.)?(?<sub>)\.?(?<domain>.+)$ and it didn't work either.
You have to escape .
~^(www\.)?(?<sub>.+?)\.(?<domain>.+)$

A regular expression to find domains that will be cached by Varnish

I ask this question here because I think this is more a regex question than an actual varnish question.
What I basically want to do, is to define a list of domains that varnish will cache. There is already an answer given to this, but I want to use a different approach.
Varnish: cache only specific domain
Now the code that is used in this answer is the following:
sub vcl_recv {
# dont cache foo.com or bar.com - optional www
if (req.http.host ~ "(www)?\.(foo|bar)\.com") {
pass;
}
# cache foobar.com - optional www
if ( req.http.host ~ "(www)?\.foobar\.com" ) {
lookup;
}
}
Now what I want is a little different. I have names with different TLD, but I only want to have the non-www version of the domain cached.
So only mydomain.com, or myotherdomain.nl, or yadomain.net
Any other subdomain can be passed to the backend.
If I'm reading your question correctly you have a list with multiple versions of the same website, so both www.foobar.com AND/OR foobar.com and you want to match ONLY foobar.com
If so, you need to back reference with (?<!www). So a search that would only match domains without the preceding www would be (?<!www\.)((?:[^.\s]+)\.(?:com|net|nl))
Hope that helps
EDIT
(?<!www\.)((?:[^.\s]+)\.(?:\w{2,3})) if domains are unknown