A
A
Alexander2017-09-19 00:25:48
Nginx
Alexander, 2017-09-19 00:25:48

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

4 answer(s)
V
Vladimir Mukovoz, 2017-09-19
@castomi

if ($http_host != "site.ru") { rewrite ^ $scheme://site.ru$request_uri? permanent; }
if ($scheme != "https") { rewrite ^ https://$http_host$request_uri? permanent;}

You can also make a separate virtual host for http and redirect from it according to this rule
And similarly make a separate virtual host for www and redirect there like this
I will explain the difference in approaches, if there are separate virtual hosts it will be more optimal. After all, if is checked every time it is opened, wasting resources, and if redirects are stuffed into virtual hosts, resources are not wasted)

E
Egor Peregudov, 2017-09-19
@Stalk-p

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]

G
Grigory Vasilkov, 2017-09-19
@gzhegow

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>

V
Viktor Taran, 2017-09-19
@shambler81

############################################################################
#### Выбор основного зеркала (с 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 question

Ask a Question

731 491 924 answers to any question