Wie man www auf nginx und symfony zwingt

887
Yamen Nassif

Ich muss domain.com immer durch www.domain.com ersetzen und https erzwingen, sodass das Endergebnis immer https://www.domain.com lautet, wenn domain.com oder www.domain.com angefordert werden. Ich habe Folgendes versucht Die Website ist weiterhin über https erreichbar, aber das WWW wird nicht erzwungen:

.zugang

RewriteCond % !^www\. RewriteRule ^(.*)$ http://www.%/$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 ein anderer Ansatz

server { listen 80; server_name domain.com; rewrite ^ https://www.domain.com$request_uri? permanent; } 

und ein paar andere Ansätze, die auf der Suche gefunden wurden, aber es scheint, dass nichts für mich funktioniert. Ich würde mich über jede Hilfe freuen. Danke im Voraus.

0

2 Antworten auf die Frage

2
unNamed

HTTP zu HTTPS:

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

Nicht-WWW zu WWW:

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

Stellen Sie sicher, dass Sie die Domains ersetzen und einen zusätzlichen serverBlock mit dem server_nameHave haben www.domain.com.

0
Yamen Nassif

Ich habe die Lösung für die Referenzbuchung gefunden:

für http

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

für https

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