Question

Nginx redirect subdomain from www to non-www

Hello, I’m using this nginx conf:

root /var/www/html/laravel/public;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name *.example.com;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }

It is multi-tenancy project, I want redirect all subdomains with www to non-www. My question is, how? :) I have one config file for all subdomains.

In my root nginx conf I just used:

server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

But in subdomains config I cant use it. My nginx conf skills are not very good, so pls help.

Thank you all

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

If someone need this, solution is:

if ($host ~* ^www\.(.*)) {
                set $host_without_www $1;
                rewrite ^(.*) http://$host_without_www$1 permanent;
}

Hello, thanks.

But I think we did not understand each other :)

What I need is redirect from www subdomain to non www subdomain, not to domain.

So:

www.sub.example.com redirect to sub.example.com
www.sub2.example.com redirect to sub2.example.com

and NOT:

www.sub.example.com to example.com

But I dont have conf files for each of this subdomains. I have only one sub nginx conf as I mentioned in my first question.

Thank you.

redirect 301 subdomain www to non www RewriteCond %{SERVER_PORT} ^80$

RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC] RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www.([^.]+.domain.com)$ [NC] RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [R=301,L]

zarrafeh.com

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.