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 Fletcher20 May 2024
Create a copy of files that go to the tmp directory
To review the content of files being generated in the /tmp directory on an Ubuntu server before Microsoft Defender removes them, you can use several approaches.  Following is the approach we took. Real-Time MonitoringYou can set up a script to monitor the /tmp directory and log the...
Andrew Fletcher01 May 2024
Common commands used in Ubuntu - in progress
A growing list of commands I've used and what they do in no specific ordersudo snap install bw ps aux | grep java whoami ip addr show uptime sudo apt update && sudo apt upgrade -y cat /etc/os-release sudo apt-get install needrestart sudo reboot sudo needrestart sudo ckan sysadmin...