E
E
Evgeniy2020-04-21 13:32:17
Apache HTTP Server
Evgeniy, 2020-04-21 13:32:17

How to reduce the number of redirects in .htaccess?

Good day to all.
Please tell me how to avoid unnecessary redirects, and can it be done on shared hosting?

The essence of the problem:
For the page - http://www.site.ru/
Redirects:
301 Moved forever https://www.site.ru/
‎ 301 Moved forever http://site.ru/
‎ 301 Moved forever https:/ /site.ru/

‎ 3 redirects, first from http to https, then when www is removed for some reason it redirects again to http , and only then to https without www

Is it possible to combine this into one redirect?
To immediately from www.site.ru to https://site.ru/ ‎ ,


removing www and changing the protocol to https htaccess

RewriteEngine On
RewriteBase /

# Переезд на https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# Убираем www 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# Блокируем доступ к файлам репозитория .hg
RewriteRule ^/?\.hg - [F,L]

# Все запросы на несуществующие файлы отправляются в index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-04-21
@dodo512

# Переезд на https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# Убираем www 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Just swap those rules.
Or replace with one rule.
RewriteCond %{SERVER_PORT} !^443$  [OR]
RewriteCond %{HTTP_HOST}   ^www\.  [NC]
RewriteRule ^(.*)$ https://site.ru/$1 [R=301,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question