P
P
Pavel2021-06-26 18:19:45
htaccess
Pavel, 2021-06-26 18:19:45

How to make single 301 redirect with htaccess?

To redirect from www and http to https, I use the following code in .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.site\.ru$ [NC]
RewriteRule ^(.*)$ https://site.ru/$1 [R=301,L]
</IfModule>


There was a need to add a redirect to the file from all pages to the main one. This solution worked:

RewriteCond %{REQUEST_URI} !"^/$"
RewriteRule (.+) https://%{HTTP_HOST}/ [R=301,L]


But if you add it to .htaccess, then redirects from all pages from www to the main one are double. That is, it https://www.site.ru/fghdgsdfleads to https://site.ru/fghdgsdf,and only then to https://site.ru/.

How to redirect www in one step?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question