Answer the question
In order to leave comments, you need to log in
How to make a 301 redirect for the entire site from http and www to https without www?
Friends, we need help. How to make a 301 redirect for the entire site and its folders in .htaccess:
Redirect example below: http://www.site.ru --> https://site.ru
Thanks in advance to everyone who helps with this problem! :)
Answer the question
In order to leave comments, you need to log in
if ($http_host != "site.ru") { rewrite ^ $scheme://site.ru$request_uri? permanent; }
if ($scheme != "https") { rewrite ^ https://$http_host$request_uri? permanent;}
Add the following code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.site.ru$ [NC]
RewriteRule ^(.*)$ http://site.ru/$1 [R=301,L]
Versatile and suitable for any project. Edit as there is a redirect to https://www., if the server is not a test one and not a local one.
# -- FORCE HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test. [NC]
RewriteCond %{HTTP_HOST} \.loc$ [NC]
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
RewriteCond %{HTTP_HOST} ^test. [NC]
RewriteCond %{HTTP_HOST} \.loc$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1/%{REQUEST_URI} [R=301,L,QSA]
RewriteCond %{HTTP_HOST} !^test. [NC]
RewriteCond %{HTTP_HOST} !\.loc$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
RewriteCond %{HTTP_HOST} !^test. [NC]
RewriteCond %{HTTP_HOST} !\.loc$ [NC]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
</IfModule>
############################################################################
#### Выбор основного зеркала (с www или без www) ####
############################################################################
# 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 если он включен.
############################################################################
#### Перенаправляем протокол https на http ####
############################################################################
#RewriteCond %{ENV:HTTPS} on
# Проверяем наличие https в URL.
#RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Перенаправляем протокол на http.
############################################################################
#### Перенаправляем протокол http на https ####
############################################################################
#RewriteCond %{ENV:HTTPS} !on
# Проверяем наличие https в URL.
#RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Перенаправляем протокол на http.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question