Forzar https
Forzar Apache Web Server a utilizar siempre HTTPS
Opción 1:
Activar el modulo:
sudo a2enmod rewrite
Si el comando anterior no funciona, instalar el modulo
sudo apt-get install -y apache2.2-common
Ingresar al directorio de apache y configurar el archivo default
sudo nano /etc/apache2/sites-available/000-default.conf
Encontraran unas lineas parecidas a esta
<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
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
↓
Los flecha abajo significa que hay mas texto debajo.
Debajo de la lineá DocumentRoot agregar la siguiente linea
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Quedando de la siguiente manera:
Ejemplo: Es solo una parte del archivo.
<VirtualHost *:80>
# Se puede eliminar todo lo que esta comentado con un #
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
↓
Reiniciar apache
sudo /etc/init.d/apache2 restart
Opcion 2:
Abrir el archivo default
sudo nano /etc/apache2/sites-available/000-default.conf
Debajo de DocumentRoot /var/www/html
<VirtualHost *:80>
# Se puede eliminar todo lo que esta comentado con un #
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
↓
La flecha abajo significa que hay mas texto debajo.
Agregar la siguiente linea
Redirect permanent / https://www.misitio.com/
Para un subdominio aplicaria lo mismo
Redirect permanent / https://subdominio.misitio.com/
Quedando de la siguiente manera:
<VirtualHost *:80>
# Se puede eliminar todo lo que esta comentado con un #
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
Redirect permanent / https://www.misitio.com/
↓
Opcion 3:
Activar el modulo:
sudo a2enmod rewrite
Dentro del directorio raíz donde se encuentren almacenados los archivos del sitio, crear un archivo .htacces
sudo nano /etc/var/www/html/.htaccess
Y agrega este contenido
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
o
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Opcion 4:
Dentro del archivo archivo index.php realizar una función que re-direccione al sitio con https.
< ?php
function redirectTohttps() {
if($SERVER['HTTPS']!=”on”) {
$redirect= “https://”.$SERVER['HTTPHOST'].$SERVER['REQUEST_URI'];
header(“Location:$redirect”); } }
?>
Opcion 5:
Dentro del archivo index.html agregar la siguiente linea, sin salirse de la etiqueta head, para re-direccionar al sitio con https.
< meta http-equiv="Refresh" content="0;URL=https://www.misitio.com" />