Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

c19a696's avatar

[FORGE] Working with www subdomain

Hi guys!

I have a website with Laravel in Forge with subdomains routes. Everything ok, but I really don't know why all www.* links are being redirected to non-www link before everything.

Is this a Nginx config? I'm using the default Nginx configuration.

Example:

  • www.example.com redirects to example.com
  • www.example.com/test redirects to example.com/test
  • sub.example.com still sub.example.com
  • anothersub.example.com still anothersub.example.com

Any idea?

Thanks

0 likes
10 replies
ryanwinchester's avatar

What is your nginx config look like?

I do the opposite of you, I have two sites on my Forge server for MyApp

myapp.com and www.myapp.com

myapp.com always redirects to www.myapp.com, as I've intended.

# myapp.com

server {
    # listen 80;
    server_name myapp.com;
    return 301 $scheme://www.myapp.com$request_uri;


    # FORGE SSL (DO NOT REMOVE!)
    # ssl on;
    # ssl_certificate;
    # ssl_certificate_key;

}

and then this (which I think is pretty much the default):

# www.myapp.com

server {
    listen 80;
    server_name www.myapp.com;
    root /home/forge/www.myapp.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl on;
    # ssl_certificate;
    # ssl_certificate_key;

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; }

    access_log off;
    error_log /var/log/nginx/www.myapp.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

1 like
psmail's avatar

I am having the same experience as @jgrossi. @fungku - did you have to manually add any of the entries above? Or did Forge set them up out of the box?

c19a696's avatar
c19a696
OP
Best Answer
Level 11

Thanks @fungku and @psmail!

I found out that Forge creates 2 virtual servers when you add a new site to a server. For example, adding testing.com to my server, going to /etc/nginx/sites-enabled I have www.testing.com and testing.com. So, the configuration inside the www.testing.com Nginx file is to redirect everything to non-www urls.

So, if you want both domains just remove the www file or if you want only www domain config the testing.com to redirect everything to www.

Thank you guys for the help! Cheers!

ryanwinchester's avatar

@psmail I added the redirect manually.

This part I think I added manually to the myapp.com nginx config file. You can do this in the forge UI.

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

image

The rest was automatically generated when you add a site in forge.

c19a696's avatar

Thanks everybody for the help :-) #laravel

Rtransat's avatar

I have the same issue. But my default vhost is the non www :

server {
    listen 80 default_server;
    server_name default;
    root /home/forge/default/public;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; }

    access_log off;
    error_log /var/log/nginx/default-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

And the www.default

server_name www.default;
return 301 $scheme://default$request_uri;

I would like always redirect with www.

ryanwinchester's avatar

@Rtransat, you should probably delete the default site and just add it as mysite.com and change www.default to www.mysite.com

lukee's avatar

Thanks guys, especially @fungku. This was helpful... I generally tip-toe nervously around nginx stuff but this was simple & to the point

nickrouty's avatar

I had to edit my config file at:

/etc/nginx/forge-conf/[domain name]/before/ssl_redirect.conf

And I removed / commented out the server block there that was re-directing requests made to the www back to the main domain. After removing that, I was able to allow the requests to hit the www without that re-direction taking place.

Please or to participate in this conversation.