如何使用查询参数将所有 www 请求重定向到非 www

How to redirect all the www requests to non www with query params

我正在尝试将所有 www 网址重定向到非 www 网址

来自 :

1. http://example.com/web-url
2. http://www.example.com/web-url
3. https://www.example.com/web-url

至:

https://example.com/web-url

我使用的配置如下。

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule . index.php

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

根据上述,一切似乎都没有问题,但第三个选项没有按预期工作。问题是 https://www.example.com/web-url 被重定向到 https://example.com/index.php

谁能告诉我我的配置有什么问题?

更改规则的顺序按预期工作。

RewriteEngine on

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule . index.php