Y
Y
Yuriy2017-07-21 14:19:31
Apache HTTP Server
Yuriy, 2017-07-21 14:19:31

Mod_rewrite How to redirect from http to https and site.com to www.site.com?

How to redirect from http to https and site.com to www.site.com?
This is how I translate to https, it works.

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

how now to transfer the site.com domain to https://www.site.com/
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTP_HOST} !^www.site.com$ [NC]
RewriteRule ^(.*)$ https://www.site.com/$1 [L,R=301]

that doesn't work
<? 
get_headers('https://www.site.com')

Warning: get_headers( http://www.site.com): failed to open stream: Connection timed out
<? 
get_headers('https://site.com')

Warning: get_headers( http://site.com): failed to open stream: Connection timed out
but the redirect itself works fine, redirects from http to http and to www.site.com

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2017-07-21
@yous

# 1. Удалить www
RewriteCond %{ENV:HTTPS} on
    #Если включен https
RewriteRule .* - [E=SSL:s]
    #То создаем переменную  ssl с текстом s
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    # Проверяем, содержит ли домен www в начале URL.
RewriteRule ^(.*)$ http%{ENV:SSL}://%1/$1 [R=301,L]
    # Перенаправляем удаляем www

    # 2. Добавить www
RewriteCond %{ENV:HTTPS} on
    #Если включен https
RewriteRule .* - [E=SSL:s]
    #То создаем переменную  ssl с текстом s
RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]
    # Если нет www в начале домена
RewriteRule ^(.*)$ http%{ENV:SSL}://www.%{HTTP_HOST}/$1 [R=301,L]
    #Подставляем www и https если он включен.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question