I have an Nginx reverse proxy that sits in front of an Apache container.
What I'm trying to do is just a simple no-www to www redirect in the htaccess file on the Apache side.
Here's the related .htaccess code in Apache:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Here's the .conf file from Nginx reverse proxy. Note some code are generated by certbot for SSL.
server {
server_name example.com www.example.com;
location / {
proxy_pass http://1.2.3.4:1234;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
My issue is, for some reason, calling example.com
or www.example.com
in the browser results in www.www.example.com
(double www).
Seems that this condition in htaccess is not being executed properly: RewriteCond %{HTTP_HOST} !^www\. [NC]
Any ideas?