text / x-generic .htaccess (текст в Юникоде UTF-8)
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 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>
Один из моих инструментов оптимизации показывает, что у меня есть несколько перенаправлений для www (HTTP), то есть с HTTP на HTTPS и еще раз с HTTPS. НА HTTPS. Как это исправить?
...which is HTTP to HTTPS and once again HTTPS TO HTTPS
Specifically, from your screenshot, you are seeing two redirects when requesting HTTP and www (ie. http://www.example.com
):
http://www.example.com
to https://www.example.com
(HTTP to HTTPS)https://www.example.com
to https://example.com
(www to non-www)No.1 is triggered by your .htaccess
rule and no.2 is no doubt being triggered by WordPress itself in PHP.
This series of redirects is actually required if you ever plan to implement HSTS (despite what your "optimization tool" is reporting).
Otherwise, you can avoid the double redirect by canonicalising the hostname in .htaccess
, before WordPress does it. One way is to add an additional rule before your existing HTTP to HTTPS redirect. For example:
RewriteEngine On
# www to non-www (and HTTPS)
RewriteCond %{HTTP_HOST} ^www\.([a-z.]+?)\.?$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
# HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The %1
in the RewriteRule
substitution is a backreference to the first captured group in the last matched CondPattern. In other words, the hostname less any www.
prefix (and any trailing dot, in the case of a FQDN).
Note that the www to non-www redirect also redirects to HTTPS. So this ensures the following HTTP to HTTPS is never processed when the first redirect occurs (so no 2nd redirect).
Make sure you clear your browser cache before testing. (To avoid caching issues it is often preferable to first test with 302 - temporary- redirects.)
Если я понимаю вашу проблему, вам следует удалить www
из {HTTP_HOST}
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
следует изменить на
RewriteCond %{HTTP_HOST} ^www\.([a-z.]+)$ [NC]<br>
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Надеюсь, это поможет вам.