# Nginx redirect domain and sub-domain without www

**URL:** <https://www.ruby-forum.com/t/nginx-redirect-domain-and-sub-domain-without-www/226742>\
**Category:** NGINX\
**Created:** [February 19, 2013, 10:55pm UTC](https://www.ruby-forum.com/t/nginx-redirect-domain-and-sub-domain-without-www/226742 "2013-02-19T22:55:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![la\_chouette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@la\_chouette](https://www.ruby-forum.com/u/la_chouette)\
**Post date:** [February 19, 2013, 10:55pm UTC](https://www.ruby-forum.com/t/nginx-redirect-domain-and-sub-domain-without-www/226742/1 "2013-02-19T22:55:27Z")

</div>

Hello,

for saas web application project I need to redirect subdomains to a  
specific  
directory (app-saas /) without the www.

The domain name must also be without redirecting to the www root.

Ex.  
www.domain.tdl to domain.tdl  
www.sub.domain.tdl to sub.domain.tdl

www  
|-- index.php (domain.tdl, without www)  
`-- app-saas/ (sub.domain.tdl, without www)

So I did this but if it does not work with: www.sub.domain.tdl

server {  
listen 80;  
server\_name ~^www.(\w+).domain.com$  
root /var/www/app-saas;

location / {  
index index.php index.html index.htm;  
try\_files $uri $uri/ /index.php?$args;  
}  
}

server {  
listen 80;  
server\_name [domain.com](http://domain.com);  
root /var/www;

location / {  
index index.php index.html index.htm;  
try\_files $uri $uri/ /index.php?$args;  
}  
}

f possible I would like to assemble her two redirect rules in a single  
parenthesis “server {…}” to apply to all common filters (header  
expire,  
deny, Hotlink, etc.)

thank you for your help, cordially.

Posted at Nginx Forum:

> **[Nginx redirect domain and sub-domain without www](https://forum.nginx.org/read.php?2,236359,236359#msg-236359)**
>
> Hello, for saas web application project I need to redirect subdomains to a specific directory (app-saas /) without the www. The domain name must also be without redirecting to the www root. Ex. www.domain.tdl to domain.tdl www.sub.d

---

<div class="post-metadata">

**Author:** ![la\_chouette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@la\_chouette](https://www.ruby-forum.com/u/la_chouette)\
**Post date:** [February 20, 2013, 12:21am UTC](https://www.ruby-forum.com/t/nginx-redirect-domain-and-sub-domain-without-www/226742/2 "2013-02-20T00:21:58Z")

</div>

On Tue, Feb 19, 2013 at 04:54:46PM -0500, la\_chouette wrote:

Hi there,

> for saas web application project I need to redirect subdomains to a specific  
> directory (app-saas /) without the www.
> 
> The domain name must also be without redirecting to the www root.

I’m afraid that I don’t understand what exactly you are asking for.

When someone asks for [http://www.example.com/file](http://www.example.com/file), what do you want to  
send back? The contents of /usr/local/nginx/html/file? Or tell them to  
ask for [http://example.com/file](http://example.com/file) instead? Or something else?

Same question for [http://www.sub.example.com/file](http://www.sub.example.com/file) –  
contents of /usr/local/nginx/html/app-saas/file; redirect to  
[http://sub.example.com/file](http://sub.example.com/file); something else?

And is that “for any subdomain they use”, or “for one specific one”?

> f possible I would like to assemble her two redirect rules in a single  
> parenthesis “server {…}” to apply to all common filters (header expire,  
> deny, Hotlink, etc.)

If the configuration really is common, then using “include” might be an  
option. But the earlier questions are probably more useful to answer  
first.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
