About Domain Redirection to WWW or Non-WWW
To redirect non-www requests to www, use this Nginx configuration:
server {
listen 443 ssl;
server_name example.com www.example.com;
charset utf-8;
if ($host ~ ^(?!www\.)(?<domain>.+)$) {
return 301 $scheme://www.$domain$request_uri;
}
# SSL certificate and other directives...
root /var/www/html;
index index.html;
}
server{
listen 80;
server_name example.com www.example.com;
location /.well-known/acme-challenge {
root /var/www/letsencrypt;
try_files $uri $uri/ =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
To redirect from www to non-www, replace the condition to this one:
if ($host ~ ^www\.(?<domain>.+)$) {
return 301 $scheme://$domain$request_uri;
}
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django App for You
Django GDPR Cookie Consent app
For Django websites that use cookies.
Django App for You