0

I'm trying to setup a nginx server which dynamically loads content from a folder for a domain. To do this I'm using regular expressions in the server name like so:

server_name ((?<subdomain>.+)\.)?(?<domain>.+)\.(?<tld>.*);

This will create a 3 variables for nginx to use later on, for example when using the following url: test.foo.example.com this will evaluate to:

  1. $subdomain = test.foo
  2. $domain = example
  3. $tld = com

The problem arises when the co.uk top-level domain is used. In this case when using the url test.foo.example.co.ukit will evaluate to:

  1. $subdomain = test.foo.cedira
  2. $domain = co
  3. $tld = uk

How can I edit the regular expression so that it will also work for co.uk?

redn0x
  • 1
  • 3

2 Answers2

1

This is able to check for the www part and also it gets just the first capture as the subdomain second capture as domain and the rest as the host-domain:

server_name ~^(www\.)?((?<subdomain>.+?)\.)?((?<domain>.+?)\.)?(?<host_domain>.+)\.(?<tld>co.uk)$ ~^(www\.)?((?<subdomain>.+?)\.)?((?<domain>.+?)\.)?(?<host_domain>.+)\.(?<tld>.*)$;
HBruijn
  • 72,524
  • 21
  • 127
  • 192
Devonte
  • 111
  • 2
  • This is able to check for the www part and also it gets just the first capture as the subdomain second caputure as domain and the rest as the host-domain – Devonte Jun 11 '15 at 12:51
  • 1
    You can always edit your own answers and do not need to comment on them to provide clarification – HBruijn Jun 11 '15 at 14:13
0

I found that it is possible to define multiple server_names, so I simply made two regular expressions as the server_name if the first one fails it will automatically go for the second one.

server_name ~^((?<subdomain>.+)\.)?(?<domain>.+)\.(?<tld>co.uk)$ ~^((?<subdomain>.+)\.)?(?<domain>.+)\.(?<tld>.*)$;

I'm guessing it can also be done with one regular expression I would still like to know how but for now I will use this solution.

redn0x
  • 1
  • 3