Andrew Fletcher published: 16 December 2021 (updated) 13 October 2023 1 minute read
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;
}
}