# Perl regex to extract "domain" and "extension" into variables from 'server\_name'

**URL:** <https://www.ruby-forum.com/t/perl-regex-to-extract-domain-and-extension-into-variables-from-server-name/238451>\
**Category:** NGINX\
**Created:** [January 10, 2014, 11:33pm UTC](https://www.ruby-forum.com/t/perl-regex-to-extract-domain-and-extension-into-variables-from-server-name/238451 "2014-01-10T23:33:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 10, 2014, 11:33pm UTC](https://www.ruby-forum.com/t/perl-regex-to-extract-domain-and-extension-into-variables-from-server-name/238451/1 "2014-01-10T23:33:07Z")

</div>

I want to extract just the “domain” and “extension” parts from an nginx  
server\_name for use as variables later in nginx conf.

For example,

server\_name \<some PERL regex … ? … ?  
…\>;  
…  
location = /test.html {  
alias /local/path/to/$domain.$extension.html;  
}

No matter what the server\_name contains, from

mydomain.tld

to

[https://www.mydomain.tld:80/something](https://www.mydomain.tld:80/something)

and variations in between,the result should be

$domain = mydomain  
$extension = tld

I found this post

“Perl Regex to get the root domain of a URL”  
[Perl Regex to get the root domain of a URL - Stack Overflow](http://stackoverflow.com/questions/15627892/perl-regex-to-get-the-root-domain-of-a-url)

that suggest this perl regex

^._://(?:[wW]{3}.)?([^:/]_).\*$

works to extract & return “domain.tld” from any from of input URI.

What’s the right form of that regex in nginx’s server\_name to populate  
 and variables for subsequent use?
