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

phpmypython's avatar

Redirect Loop for non-www domains to www domain.

Im working on trying to forward all non-www request to www request for a site i setup in forge.

This is my nginx configuration

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

server {
    listen 80;
    server_name www.domain.com;
    root /home/forge/www.domain.com/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/www.domain.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        proxy_intercept_errors on;
        error_page 502 = @fallback;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root;
        include fastcgi_params;
    }

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

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

I know that forge will auto generate a file with www prepended to what ever site you make so i removed that one and now there's only one conf file that should be firing when i visit the domain. If i do a wget on the non-www domain then i can see that it's hitting the 301 but then when it begins a new connection with the www domain it 301's back to the non-www domain thus causing the redirect. Im not sure where ive messed up here and i would really appreciate some help

0 likes
8 replies
manshu's avatar

Were you able to resolve this issue ? Im having similar problem.

Shovels's avatar

@phpmypython and @manshu - I've had a look at the issue - when you create a site using Forge it actually creates two config files. One for www and one for non-www.

The www file contains:

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/www.yourdomain.co.uk/before/*;

server {
    listen 80;
    server_name www.domain.co.uk;
    return 301 $scheme://yourdomain.co.uk$request_uri;
}

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/www.yourdomain.co.uk/after/*;

The non-www file is the one you see if you edit the nginx.conf file from within Forge.

To edit these files you'll need to SSH into your server and update the domain prefixes.

cd /etc/nginx/sites-available/
sudo nano www.yourdomain.co.uk

Then change

server {
    listen 80;
# from    server_name www.yourdomain.co.uk; 
    server_name yourdomain.co.uk;

# from    return 301 $scheme://yourdomain.co.uk$request_uri;
        return 301 $scheme://www.yourdomain.co.uk$request_uri;
}

Save and exit (Ctrl + x) Then edit the non-www file

sudo nano yourdomain.co.uk

And update the one line shown below

server {
    listen 80;
# from server_name yourdomain.co.uk
    server_name www.yourdomain.co.uk;
    root /home/forge/yourdomain.co.uk/public;
#    ... The rest of the config file remains unchanged

Save and exit (Ctrl + x)

Now, to get the correct config file to show up in Forge you'll need to swap the file names.

sudo mv www.yourdomain.co.uk tmp.yourdomain.co.uk
sudo mv yourdomain.co.uk www.yourdomain.co.uk
sudo mv tmp.yourdomain.co.uk yourdomain.co.uk

You'll then need to restart Nginx. I'd run a test first to make sure you've not borked anything...

sudo service nginx configtest
# hopefully this should show [OK]
# Then restart Nginx
sudo service nginx restart
1 like
stueynet's avatar

I figured I would bump this now as it seems forge has changed its nginx setup. Also given everyone uses ssl now since its basically automatic, it would be great to see the ssl version of this fix.

When you set up a new site in forge, there only appears to be a single nginx config file at /etc/nginx/sites-available/sitename.com. Once you ssl that it looks like:

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/sitename.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name sitename.com;
    root /home/forge/sitename.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/sitename.com/182548/server.crt;
    ssl_certificate_key /etc/nginx/ssl/sitename.com/182548/server.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'SHA-...';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/sitename.com/server/*;

    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/sitename.com-error.log error;

    error_page 404 /index.php;

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

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

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/sitename.com/after/*;

What would be the new steps to adjust the above config to force www?

stueynet's avatar

Ok to anyone who is listening I have this all figured out. When you enable ssl for a domain a new file is added at /etc/nginx/forge-conf/domain.com/before/ssl_redirect.conf. The is run before your normal nginx file located at /etc/nginx/sites-available/domain.com. So in the new file you need to do the following:

Change:

server_name .domain.com;

to

server_name domain.com;

Closer to the bottom change:

server_name www.domain.com;
return 301 https://domain.com.com$request_uri;

to

server_name domain.com;
return 301 https://www.domain.com.com$request_uri;

And final back in your /etc/nginx/sites-available/domain.com file change:

server_name domain.com;

to

server_name www.domain.com;
6 likes
neovive's avatar

Taylor should consider adding this as a configuration option from the Forge dashboard. The Dreamhost panel has a simple radio button, where you can select your preferred domain redirect option (example.com or www.example.com).

5 likes
dominykasgel's avatar

@stueynet Just wanted to say a big thank you! I went over stackoverflow, etc, and did many changes until I found your reply with a proper solution :) Crazy Forge.

antimech's avatar

Guys, you could just add www. in "Meta" section of your Forge site. Yeah, that's it.

1 like
gbrits's avatar

Thanks, in my particular case I just changed it from non-www to www & ditched the alias of www and my issues were rectified. Wordpress tends to make things a little more complicated when it comes to site migrations.

Please or to participate in this conversation.