I need to always replace domain.com with www.domain.com and to force https so the final result should always be https://www.domain.com when domain.com or www.domain.com are requested i tried the follows the website is still accessible with https but www is not being forced:
.htaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
nginx
server {
listen 80;
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.domain.com;
root /path/to/project;
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
location ~ /\. {
deny all;
}
location ~ /.well-known {
allow all;
}
error_log /path/to/error.log;
access_log /path/to/access.log;
}
server {
listen 443 ssl;
server_name www.domain.com;
..rest of code..
}
nginx another approach
server {
listen 80;
server_name domain.com;
rewrite ^ https://www.domain.com$request_uri? permanent;
}
and couple of another approaches found on search but it seems nothing is working for me i would appreciate any help. Thanks in advance.