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

daveb2's avatar
Level 3

Unable to get subdomain of hosted site

I'm not sure if this is a forge issue or a laravel issue. I suspect it's something to do with Forge's wildcard virtual hosts option and the way it configures nginx.

I have configured a Ubuntu droplet on Digital Ocean via forge, and I'm hosting a site at:

www.mysite.com.au

However, I want to detect whether the hostname includes the "www" or not, and currently all of the ways I have of checking return mysite.com.au - even when I can see the www in the address bar of the browser.

Here are some of the things I have tried:

gethostname();
url()->current();
request()

These functions all return mysite.com.au instead of the expected www.mysite.com.au

This is on an upstream server, behind a load balancer, which is running on another DO droplet using a forge load balancer config.

The reason I want to detect the www. prefix is because I want to redirect to the non-www version of the site to fix a lot of CORS errors, caused by includes of js and css assets the reference the non-www version of the path. I realise that this should be done in the nginx config, but I am trying to stick to the default forge configs as much as possible for ease of management.

0 likes
3 replies
LaryAI's avatar
Level 58

To address the issue of detecting the "www" subdomain and redirecting to the non-www version of your site, you can handle this at the Nginx configuration level. This approach is more efficient and reliable than trying to detect the subdomain within your Laravel application.

Here's how you can configure Nginx to redirect all "www" requests to the non-www version of your site:

  1. Access your Nginx configuration file: This is typically located at /etc/nginx/sites-available/your-site or a similar path, depending on your server setup.

  2. Add a server block for the www subdomain: You need to create a server block that listens for requests to the "www" subdomain and redirects them to the non-www version.

Here's an example configuration:

server {
    listen 80;
    server_name www.mysite.com.au;

    return 301 $scheme://mysite.com.au$request_uri;
}

server {
    listen 80;
    server_name mysite.com.au;

    # Your existing configuration for the non-www version
    root /path/to/your/application/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust PHP version as needed
    }

    # Other configurations...
}
  1. Reload Nginx: After making changes to the configuration file, reload Nginx to apply the changes:
sudo nginx -t  # Test the configuration for syntax errors
sudo systemctl reload nginx  # Reload Nginx

This configuration will ensure that any request to www.mysite.com.au is redirected to mysite.com.au, which should help resolve the CORS issues you're experiencing due to mixed content.

By handling the redirection at the Nginx level, you avoid the need to detect the subdomain within your Laravel application, simplifying your code and improving performance.

daveb2's avatar
Level 3

@LaryAI Thanks LaryAI but I have tried this approach several times already to no success.

daveb2's avatar
Level 3

The server config route didn't work for me. Eventually I just configured a Cloudflare redirect rule, and that is working well so far.

Please or to participate in this conversation.