redirigir a htaccess https://www


Tengo el siguiente código htaccess:

<IfModule mod_rewrite.c>

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

</IfModule>

Quiero que mi sitio sea redirigido a https://www. con HTTPS, y hacer cumplir el subdominio www. , pero cuando acceso http://www. (sin HTTPS), no me redirige a https://www con HTTPS.

Author: Michael Berkowski, 2012-12-20

12 answers

Para forzar primero HTTPS, debe verificar la variable de entorno correcta %{HTTPS} off, pero su regla anterior precede a la www. Ya que tiene una segunda regla para hacer cumplir www., no la use en la primera regla.

RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Acerca de proxying

Cuando está detrás de algunas formas de proxy, por lo que el cliente se está conectando a través de HTTPS a un proxy, balanceador de carga, aplicación de pasajeros, etc., la variable %{HTTPS} nunca puede ser on y causar un bucle de reescritura. Esto se debe a que su aplicación es en realidad recibir tráfico HTTP simple aunque el cliente y el proxy/balanceador de carga estén usando HTTPS. En estos casos, compruebe la cabecera X-Forwarded-Proto en lugar de la variable %{HTTPS}. Esta respuesta muestra el proceso apropiado

 523
Author: Michael Berkowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-02 20:32:38

La respuesta de Michals funcionó para mí, aunque con una pequeña modificación:

Problema:

Cuando usted tiene un certificado de seguridad de un solo sitio, un navegador que intenta acceder a su página sin https: / / www. (o cualquier dominio que cubra su certificado) mostrará una fea pantalla roja de advertencia antes de que incluso reciba el redireccionamiento a la página https segura y correcta.

Solución

Primero use la redirección a la www (o cualquier dominio cubierto por su certificado) y solo entonces hacer el redireccionamiento https. Esto asegurará que sus usuarios no se enfrenten a ningún error porque su navegador vea un certificado que no cubre la url actual.

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 132
Author: Larzan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-18 10:36:05

Si está utilizando CloudFlare o una CDN similar, obtendrá un error de bucle infinito con las soluciones %{HTTPS} proporcionadas aquí. Si eres un usuario de CloudFlare necesitarás usar esto:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 89
Author: Andrew,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-13 23:30:44

MALA SOLUCIÓN Y POR QUÉ!

Cuando se utiliza el código de la respuesta aceptada:

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

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]

El navegador va a:

http://example.com

Luego redirige a:

https://example.com

Luego redirige a:

https://www.example.com

Esto es demasiada solicitud al servidor


LA MEJOR SOLUCIÓN Y LA RESPUESTA

Este código tiene una condición [OR] para evitar cambios duales en la url!

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]

La mayoría de las respuestas, incluso una aceptada, no utilice este truco.

 34
Author: Amir Forsati,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-27 08:46:37

Hay muchas soluciones por ahí. Aquí hay un enlace a la wiki de apache que trata este problema directamente.

Http://wiki.apache.org/httpd/RewriteHTTPToHTTPS

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
 23
Author: Vynz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-23 07:47:33

Esta es la mejor manera que encontré para los usuarios proxy y no proxy

RewriteEngine On

### START WWW & HTTPS

# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

### END WWW & HTTPS
 23
Author: llioor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-09 16:09:30

Para redirigir http:// o https:// a https://www puede utilizar la siguiente regla en todas las versiones de apache :

RewriteEngine on

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]

Apache 2.4

RewriteEngine on

RewriteCond %{REQUEST_SCHEME} http [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]

Tenga en cuenta que la variable %{REQUEST_SCHEME} está disponible para su uso desde apache 2.4 .

 9
Author: starkeen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-28 16:52:48

Si está en CloudFlare, asegúrese de usar algo como esto.

# BEGIN SSL Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# END SSL Redirect

Esto lo salvará del bucle de redirección y redirigirá su sitio a SSL de forma segura.

P.d. Es una buena idea comprobar si el mod_rewrite.c!

 8
Author: Ahmad Awais,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-10 04:52:41

Establecer en su .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 0
Author: Adam Kozlowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-25 14:17:22

Intento la primera respuesta y no funciona... Este trabajo:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
 0
Author: Jackssn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-30 14:45:25
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Notas: asegúrese de haber realizado los siguientes pasos

  1. sudo a2enmod reescribir
  2. sudo service apache2 restart
  3. Agregue lo siguiente en su archivo vhost, ubicado en /etc/apache2/sites-available/000-default.conf
<Directory /var/www/html>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  allow from all
  Require all granted
</Directory>

Ahora tu .htaccess se trabajo y su sitio redireccionará a http: / / a https://www

 0
Author: Kundan roy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-07 21:48:37

Esto funcionará tanto para https como para www

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
 -2
Author: Nadeem As,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-20 16:14:49