Skip to main content

Forcing https and www or non-www is a process that I was a custom to through .htaccess.  In fact I had become very strong at managing and working my .htaccess files.  However, what I had become strong in one area, I was oblivious to using other methods.  My bad.

Well that was until I had to change my way.  Working on an Nginx server, .htaccess was not in play.  Instead, I needed to configure the /etc/nginx/sites-available directory.

For me to force SSL and www I added the following

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

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    ssl_certificate /etc/ssl/certs/{project}2122.crt;
    ssl_certificate_key /etc/ssl/private/project}_WC_21-22_plain.key;
    server_name {domain};
    return      301 https://www.codebales.com$request_uri;
}

server {
    listen 80;
    listen [::]:80;
        listen 443 ssl;
        ssl_certificate /etc/ssl/certs/Code2122.crt;
        ssl_certificate_key /etc/ssl/private/Code_WC_21-22_plain.key;
    root /var/www/html/drupal;
    index  index.php index.html index.htm;
    server_name  {domain};

    # Redirect HTTP to HTTPS
    if ($scheme = http) {
        return 301 https://$server_name$request_uri;
    }

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

    location @rewrite {
               rewrite ^/(.*)$ /index.php?q=$1;
        }

    location ~ [^/]\.php(/|$) {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }

    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
               try_files $uri @rewrite;
        }

    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
        }
}

 

Related articles

Andrew Fletcher08 Apr 2025
How smart blocking protects your digital infrastructure
Across every industry, the operational risks of cyber threats are escalating. Automated bots, denial-of-service attacks and vulnerability scanners are increasingly common. For businesses operating in Australia and globally, implementing resilient, proactive security measures is essential to ensure...
Andrew Fletcher21 Nov 2024
How to update your Ubuntu server efficiently
Maintaining your Ubuntu server is essential to ensure it operates smoothly, stays secure, and benefits from the latest features. Whether you're managing a server for personal projects or enterprise-level applications, regularly updating your system is a critical best practice. Here’s a...