How to force https and www on symfony 4?

by herminia_bruen , in category: PHP Frameworks , 6 months ago

How to force https and www on symfony 4?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , 6 months ago


To force HTTPS and www on Symfony 4, you can follow the steps below:

  1. Ensure that your server is already configured to use HTTPS and has a valid SSL certificate installed.
  2. Open the .htaccess file located in the public folder of your Symfony 4 project.
  3. Add the following lines of code at the beginning of the file to force HTTPS redirection:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  1. To force www redirection as well, add the following lines of code after the HTTPS redirection code:
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  1. Save the .htaccess file.

By following these steps, all requests to your Symfony 4 application without HTTPS or without www will be redirected to the secure version with www.