0

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.

2 Answers 2

2

HTTP to HTTPS:

server {
        listen 80;
        server_name *;
        location / {
                return 302 https://$host$request_uri;
        }
}

Non-WWW to WWW:

server {
        server_name domain-without-www.com;
        listen 443 ssl;
        location / {
                return 302 https://www.$host$request_uri;
        }
}

Make sure to replace the domains and having an extra server block with the server_name having www.domain.com.

0

i found the solution for reference posting :

for http

location / {
           if ($http_host !~ "^www\."){
                  rewrite ^(.*)$ http://www.$http_host$1 redirect;
           }
           try_files $uri /app.php$is_args$args;
}

for https

location / {
        if ($http_host !~ "^www\."){
                rewrite ^(.*)$ https://www.$http_host$1 redirect;
        }
        try_files $uri /app.php$is_args$args;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.