S
S
Shazel2015-01-15 20:49:57
htaccess
Shazel, 2015-01-15 20:49:57

How to set up a redirect to https in .htaccess?

In general, the site on Wordpress, in htasses now is this

# 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


Https loads well and http not so much. I think to create a redirect to https so that people from search engines are immediately where they need to be. Tried different options, but all create a circular redirect.

In general, throw in an example code. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

19 answer(s)
G
Geographer, 2016-03-30
@Geographer

https://www.reg.ru/support/hosting-i-servery/sajty...

RewriteEngine On
RewriteCond %{HTTPS} =off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

if both of the first options did not help and a circular redirect occurs :
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

S
spieldy, 2016-02-26
@spieldy

here is another option

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

E
Eugene, 2017-01-23
@Snop

Who DOES NOT help anything, like me.
This site online-generators.ru is connected to CloudFlare . There were problems with him.
It was decided this way: we go into the CloudFlare account in the Page Rules section. There we set the rule to redirect all pages of the site to a secure protocol.

O
Oleg Prilepa, 2016-02-27
@OAPrilepa

RewriteCond %{HTTP:X-HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

R
Roman, 2017-12-19
@romasport

masterhost

RewriteCond %{HTTP:PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

I
IceJOKER, 2015-01-15
@IceJOKER

https://www.sslshopper.com/apache-redirect-http-to...

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

O
Oleg Kobrin, 2018-04-18
@Boydzila

Hosting active.by (if anyone needs it):

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

T
tarasui, 2019-03-22
@tarasui

for hts.ru hosting

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-SSL} !^yes$
RewriteRule (.*) https://gu77.ru/$1 [R=301,L]
</IfModule>

A
Andrey Yastrebov, 2017-09-03
@flexodj

For Gino Hosting

RewriteEngine On  # Если этой строки нет выше
RewriteCond %{HTTP:X-Forwarded-Protocol} !=https
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

D
Dron84, 2019-10-14
@Dron84

This option helped me.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

But this is for SPA But as for me, the option should help you
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

E
Eugene, 2016-03-14
@CB9TOIIIA

Helped me like this:

RewriteEngine On
RewriteCond %{HTTP:SSL} !1
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L,R=301]

A
Andrews32, 2017-02-22
@Andrews32

For a static / self-written site in PHP (not Wordpress) on Timeweb hosting, the code helped me:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru[nc]
RewriteRule ^(.*)$ https://site.ru/$1 [r=301,nc]
# или RewriteRule ^(.*)$ https://www.site.ru/$1 [r=301,nc] если нужно с www

O
Oleg Prilepa, 2018-07-03
@OAPrilepa

Using the free Let's Encrypt SSL certificate, the rule should end up looking like this:

# SSL
  RewriteCond %{REQUEST_FILENAME} !^/\.well-known(.*)$
  RewriteCond %{HTTP:X-HTTPS} !1
  RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

A
Alexey Fisenko, 2018-08-15
@So1omon

Here is a working version

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

BUT, for everything to work, you need to insert this code at the very top of the .htaccess file

N
Niku0201, 2020-07-19
@Niku0201

After adding this code to .htaccess, the following problem may occur:
"website *site.name* redirected too many times."
To avoid this, you need to add the following not to .htaccess, but to wp-config:

define('FORCE_SSL_ADMIN', true);
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') 
$_SERVER['HTTPS']='on';

Personally, this helped me and now the site opens as it should and the redirect goes from http to https without problems and errors.
Good luck gentlemen.

R
re1mond, 2020-09-30
@re1mond

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Protocol} !=https
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

E
Eugene Simon, 2017-05-30
@Comers

Nothing came up

V
Vladimir Shabliy, 2020-04-23
@vovka3003

Temporarily threw the new engine into the folder below the root of the old site and only "Option 3" from Geograph helped (the rest threw it to the root) .
Thank you))

D
Denis, 2020-06-12
@Drake4ree

For reg.ru working version on 2020-06-12

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question