Redirect from HTTP to HTTPS with htaccess

0

Let's see if any partner takes me out of doubt.

I'm about to launch a new website designed with php and I want to modify the .htaccess file to use the SSL security certificate.

I have found so many examples that I do not know how to use . Someone would be so kind and I could explain if it is possible in detail what each line of the codes I show below means or what each one is for.

Example 1: This uses (IfModule) .

<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R=301,L,QSA]
</IfModule>

Example 2:

# Enviar trafico HTTP a HTTPS
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{HTTP_HOST} ^tu_dominio\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.tu_dominio\.com$
RewriteRule ^(.*)$ https://tudominio.com/$1 [R=301,L,NE]

And in some cases they recommend adding the following lines, which are not used either:

SSLOptions + StrictRequire
SSLRequireSSL

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

The one who can answer my questions will be very grateful to you.

Thank you very much for your time.

Greetings.

    
asked by juan pablo 19.12.2018 в 19:29
source

1 answer

2

use this is the simplest and it will take you out of trouble.

PS: In the comments I explain to you what each one serves.

RewriteEngine On

#Redireccionar a https cuando tiene www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Redireccionar a https con www cuando no tiene https ni www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.tupagina.com%{REQUEST_URI} [L,R=301]

#Redireccionar a www cuando tiene https
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.tupagina.com%{REQUEST_URI} [L,R=301]
    
answered by 19.12.2018 / 19:54
source