[Symfony 4.4] Comment faire une redirection sans le .htacces

Par Devdeb22, il y a 5 ans


Bonjour les amis, voilà j'arrive enfin au bout de mon autoformation sur Symfony

J'ai pris un serveur VPS et installé mon projet Symfony dessus et il fonctionne très bien.

J'ai aussi installé le ssl et il fonctionne aussi.

Quand je tape https dans le navigateur ça fonctionne aussi.

Le problème que je rencontre c'est que quand je tape seulement "monsite.com" sans le https, je tombe sur la version en http et pas sur le https.

Donc il faut que je fasse une réécriture d'url.

Le soucis c'est que je n'arrive pas à faire ça sans le fichier .htaccess.

J'ai configuré le Vhost mais ça fonctionne pas.

Quelqu'un peut jeter un petit coup d'oeuil et me dire s'il comprend pourquoi ça ne fonctionne pas

voici mon Vhost

PS : Mon serveeur tourne sous Debian 10

<IfModule mod_ssl.c> <VirtualHost *:443> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerName dev.monsite.com ServerAlias www.dev.monsite.com ServerAdmin webmaster@monsite.com DocumentRoot /var/www/monsite.com/dev/public <Directory /var/www/monsite.com/dev/public > #Require all granted #AllowOverride All #Options Indexes RewriteEngine on Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all #RewriteRule ^(.*) /public/$1 [L] #RewriteCond %{HTTP_HOST} ^monsite.com </Directory> # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf # Some rewrite rules in this file were disabled on your HTTPS site, # because they have the potential to create redirection loops. RewriteCond %{SERVER_NAME} =www.monsite.com [OR] RewriteCond %{SERVER_NAME} =monsite.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/dev.monsite.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/dev.monsite.com/privkey.pem </VirtualHost> </IfModule> <IfModule mod_ssl.c> <VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerName dev.monsite.com ServerAlias www.dev.monsite.com ServerAdmin webmaster@monsite.com DocumentRoot /var/www/monsite.com/dev/public <Directory /var/www/monsite.com/dev/public > #Require all granted #AllowOverride All #Options Indexes RewriteEngine on Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all FallbackResource /index.php #RewriteRule ^(.*) /public/$1 [L] #RewriteCond %{HTTP_HOST} ^monsite.com </Directory> <Directory /var/www/monsite.com/dev/public/bundles> FallbackResource disabled </Directory> # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf # Some rewrite rules in this file were disabled on your HTTPS site, # because they have the potential to create redirection loops. RewriteCond %{SERVER_NAME} =www.dev.monsite.com [OR] RewriteCond %{SERVER_NAME} =dev.monsite.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost> </IfModule>

4 réponses

Kareylo, il y a 5 ans

Bonjour,

Dans ta partie <VirtualHost *:80>, il faut que tu rajoutes une redirection permanente :

Redirect permanent / https://dev.monsite.com
Devdeb22, il y a 5 ans

Un grand merci.
J'ai rajouté ces deux lignes là

RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Et ça fonctionne !

Il me reste cependant un dernier soucis, je n'arrive pas à faire appairaitre la barre de debug et mes Route ne fonnectionnent pas

J'ai ce message qui s'affiche

An error occurred while loading the web debug toolbar.

J'ai beau faire la commande

composer require symfony/profiler-pack

ça ne change rien !

Devdeb22, il y a 5 ans

Un grand merci, j'ai trouvé la solution.

Il fallait ajouté ceci au Vhost que ça soit pour l'écoute du port 443 que l'écoute du port 80

RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
bidule, il y a 5 ans

Hello,

Si tu as passé ton site en prod et non plus en dev où que tu as un .env.local.php tu n'auras plus la debug bar.

Elle n'est dispo qu'en mode DEV