php - Redirect http to https in .htaccess (Symfony 4) ← (PHP, Symfony, HTML)

I'm trying to host my symfony 4 website in a shared hosting (Cpanel), and i want to redirect http to https automatically.

I found on many questions here is that i should change mu .htaccess, here is my current .htaccess content :

DirectoryIndex index.php

AddType application/x-lsphp72 .php

<IfModule mime_module>
  AddType application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>

<IfModule php7_module>
   php_flag display_errors On
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 128M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php72"
   php_value upload_max_filesize 10M
   php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
   php_flag display_errors On
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 128M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php72"
   php_value upload_max_filesize 10M
   php_flag zlib.output_compression Off
</IfModule>

<IfModule mime_module>
  AddHandler application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>

I found on this question Redirect HTTP to HTTPS that i should add :

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]

but i got ERR_TOO_MANY_REDIRECT error.

Any of you guys got the same thing as me ?

Thanks for helping.

Answer



Solution:

Rather than the .htaccess, I would suggest to modify the virtual host
(config file in /etc/apache2/sites-available)

Do this and you're set :

# Catch and redirect all http
<VirtualHost *:80>
    ServerName example.com

    Redirect / https://example.com/
</VirtualHost>

<VirtualHost _default_:443>
    ServerName example.com

    # Rest of your vhost
</VirtualHost>

Answer



Solution:

I resolved this issue via security.yaml

 access_control:
     - { path: ^/my-path, requires_channel: https }

now when I access to http://my-website.com/my-path automatically I am redirected to https

Source