I
I
IwanIwanow2021-03-19 13:44:27
htaccess
IwanIwanow, 2021-03-19 13:44:27

.htaccess rules. Why is a chain of redirects formed and the main page at www. gives response code 302 instead of 301?

I audit the site using ahrefs.com and see:
605481b0438ea522601523.jpeg
As far as I understand, from htр://www . The main page first has a temporary 302 redirect to https://www . main, and from it to https:// main (without www), although immediately with httр://www . https:// (without www) should go.

.htaccess content:

# 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
# BEGIN Ahrefs
Redirect 301 http://mysite.ru/ https://mysite.ru/
Redirect 301 http://www.mysite.ru/ https://mysite.ru/
Redirect 301 https://www.mysite.ru/ https://mysite.ru/
# END Ahrefs
#RewriteEngine On  # Если этой строки нет выше
RewriteCond %{HTTP:X-Forwarded-Protocol} !=https
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]


Please explain the redirect rules from my .htaccess.
1) Separately set Redirect 301 for all cases with the main page, so that a redirect would occur immediately on https://mysite.ru/ without a chain, but as far as I understand, this 301 is lower in priority than other rules, so it does not get rid of the chain .
2) As far as I understand, RewriteCond %{HTTP:X-Forwarded-Protocol} !=https is a rule for cases when the protocol is not https. It will fire for the cases listed below it, but until the next RewriteCond?
3) Does the RewriteCond from the WordPress block conflict with the last RewriteCond?
4) Will rearranging my three Redirect 301s to the very top help increase their priority and get rid of the chain?
5) In addition to the chain, withhttр://mysite.ru redirect to httрs://mysite.ru also comes with 302 code - why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vintets, 2021-03-19
@IwanIwanow

I'm certainly an expert, but why are you putting these redirects after the WordPress block? After all, there must be before.
Separate redirects for the main page are superfluous, they should be processed according to general rules.
I did this myself:

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

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

first part: if the address with www (both http and https) redirects to https without www
second: if not https, redirects to https (without www)
It seems to redirect only once in any case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question