3

Basically, I'm trying to setup nginx with regex so that it:

  • Matches all subdomains for pyronexus.com & notoriouspyro.com but NOT www (www is redirected to pyronexus.com).
  • Gives me a variable I can use to determine what the subdomain and domain entered are (e.g. if someone enters space.pyronexus.com, I would like to have two variables $subdomain and $domain containing space and pyronexus).

So far, I have this: ~^(.*)\.(?:pyronexus|notoriouspyro)\.com$

But I just can't seem to figure out anything else! Any help would be greatly appreciated.

Edit: Perhaps it would help to show my nginx config file:

server {
    server_name pyronexus.com notoriouspyro.com;
    listen 127.0.0.1:80 default_server;

    root /home/nginx/pyronexus.com/public;
    index index.html index.php;

    access_log /home/nginx/pyronexus.com/logs/access.log;
    error_log /home/nginx/pyronexus.com/logs/error.log;

    include php.conf;
}

server {
    server_name ~^(www\.)?(.+)$;
    listen 127.0.0.1:80;

    return 301 $scheme://$2$request_uri;
}

The first part is the server which I need the regex for, the second part is to try and catch all domains landing on www and redirect them without www.

NotoriousPyro
  • 377
  • 2
  • 12

3 Answers3

1

This pattern seems to do it:

^((?!www).+?)\.(?:pyronexus|notoriouspyro)\.com$

Courtesy of Regular expression to match a line that doesn't contain a word?

Tested here:

http://regex101.com/r/yK7oE2/1

In case you need the domain name, simply leave out the ?::

^((?!www).+?)\.(pyronexus|notoriouspyro)\.com$
Community
  • 1
  • 1
MightyPork
  • 16,661
  • 9
  • 66
  • 120
1

Take a step back. The task is:

  • serve sites on pyronexus.com and notoriouspyro.com
  • redirect subdomains to their respective domains
  • redirect www subdomains to pyronexus.com

So instead of fiddling with an overly complex regular expression, make three server blocks. The second in the task list is the catch all.

Melvyn
  • 8,808
  • 1
  • 22
  • 36
1

This is pretty easy, like @Melvyn said, you are over thinking this, you need a catch all server to handle all domains, then create a specific server for redirecting the www.

The best variable you want to know the host you are accessing is $http_host

server {
  listen 80 default_server;
  # here handle all subdomains, this will also match the non-www domains of
  # the both domains
}
server {
  listen 80;
  server_name www.pyronexus.com;
  return 301 http://pyronexus.com$request_uri;
}
server {
  listen 80;
  server_name www.notoriouspyro.com;
  return 301 http://notoriouspyro.com$request_uri;
}
Mohammad AbuShady
  • 35,036
  • 9
  • 71
  • 86
  • I decided to go with server_name ~^subdomain\.(pyronexus|notoriouspyro)\.com$; for each subdomain, and then have the www and main domains handled similarly to how you done it. – NotoriousPyro Jul 08 '14 at 20:00
  • 1
    It would be a tiny bit better if you did 2 server names, `server_name subdomain.pyronexus.com subdomain.notoriouspyro.com;` because this would be easier to match for nginx rather than matching by regex, since you're not really trying to capture any thing, wouldn't really matter at first, but could lift a small load if the website has a lot of traffic. – Mohammad AbuShady Jul 09 '14 at 09:34