Regular Expressions: Get subdomain and domain - regex

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>.+)$

Related

Regular expression for Google analytics for matching the domain name in a URL

I am too new to regular expressions and my effort at it so far seems futile. I'm trying to write a very simple regexp to match a domain name like google.com with any URL referrer like https://www.google.com.
So far I have tried /*google.com*/, (*google.com*) and [*google.com*] and none of these have worked.
Any help would be greatly appreciated.
Try the below regular expression. I assume you are applying this on the Hostname dimension.
.*google\.com.*
I wrote you a regex which matches prefixes, such as e.g.: https, http, ftp & sftp.
The regex can be found here:
http://rubular.com/r/7sLhefh2xE

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)\..*

How to write regex for apache ProxyPassMatch to reverse proxy API calls

I have an angular 4 web application which is hosted on apache 2.4. The application makes use of an API written in nodejs javascript running over express. Both the website and the API service are running on the same machine but on different ports. The website is on port 80 and the API service is listening on port 9000.
I would like to set up apache to do reverse proxy for all the API calls.
For example, any url that contains /api/ I want it rewritten by apache to point to the API url:port. If I use ProxyPass like the following lines, the redirect works fine:
ProxyPass "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
ProxyPassReverse "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
What I do not know how to do, is to use the ProxyPassMatch directive and create a regular expression so that any url that contains /api/ is redirected to http://localhost:9000/api/.....
I tried the following but it does not work:
ProxyPassMatch "^/api.*$" "http://localhost:9000/$1"
ProxyPassReverse "^/api.*$" "http://localhost:9000/$1"
Neither does the following:
ProxyPassMatch "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
ProxyPassReverse "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
Any help would be appreciated. My regex skills are lacking!
Note: obviously 'localhost' can be an IP address or a domain, I am using it in the example for simplicity.
Many thanks!
Edit: I corrected the first example to use .* instead of just * as per Alex's comment.
I solved the problem. The correct way to do reverse proxy with apache on the above example is the following:
ProxyPassMatch "/api(.*)" "http://localhost:9000/api$1"
ProxyPassReverse "/api(.*)" "http://localhost:9000/api$1"
I knew the multiple regex examples I was trying were correct, as I was testing them with https://regex101.com/, but I was hard coding the second part of to a particular route in order to eliminate the issue of the second part being incorrect, but for some reason it does not like that. Once I understood that the (.*) part of the regex is the first capture group and used it as $1 in the second part, it all worked.
I hope I clarified the answer enough and it is useful to someone else.

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

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]+$

Sitecore Multiple Sites - Using Wildcards With hostName Attribute

I'm having an issue with getting a site set up in the web.config file for a Sitecore site. Specifically I can't figure how to use the hostName property to capture the "www" subdomain for a domain (e.g. www.mydomain.com) as well as no specified subdomain (e.g. mydomain.com).
I've experimented a little and found that I can do something like *.mydomain.com and it works. But the problem is that we want users to also be able to go to just mydomain.com and have the site come up. When I have the hostName configured as *.mydomain.com this apparently is not possible.
Any ideas? The Sitecore developer network doesn't say too much on this (unless it's hidden somewhere I couldn't find).
Craig
For a bit more precision than Mark's removing the dot (which will work) you can use pipe separation to list alternative names:
<site hostName="mydomain.com | *.mydomain.com" ... />
That would allow you to configure a second site reallymydomain.com without it being caught by the hostName above. Remember the sites list will be processed in order, so the first match counts even if there's a second match that is more specific.
Try no dot in the hostName:
<site hostName="*mydomain.com" ... />
Both Mark and James answers are correct and will help you resolve multiple domain/subdomain names to a single Sitecore site.
You may instead want to consider setting up a redirect in IIS from the non www domain to the www sub-domain or vice-versa. Having more than one definitive URL for your domain can negatively effect your page rank.
This is a handy module for IIS 7 to help you define redirects. http://www.iis.net/download/urlrewrite