如何将 www 重定向到非 www https?
How to redirect www to non-www https?
我已经在我的域上安装了 https。所有这些案例都可以在我的网站上找到:
http://example.com // will be redirected to https://example.com
https://example.com
http://www.example.com // will be redirected to https://www.example.com
https://www.example.com
看到了吗?一切都将使用 https 协议。一切顺利。
现在我需要将所有 www
重定向到 non-www
。所以 http://www.example.com
和 https://www.example.com
应该重定向到 https://example.com
(换句话说,所有情况都应该重定向到这个).
这是我当前在 /etc/apache2/sites-avalible/000-default.conf
中的代码:
<VirtualHost *:80>
.
.
.
RewriteEngine on
RewriteCond %{SERVER_NAME} =lamtakam.com [OR]
RewriteCond %{SERVER_NAME} =www.lamtakam.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] -- this
RewriteRule ^(.*)$ https://%1/ [R=301,L] -- and this are for www redirection
-- but doesn't work
</VirtualHost>
有什么想法吗?
另外 lamtakam 是我所说的确切域,如果您需要检查某些内容。
编辑:
目前我的项目根目录下的 .htaccess
文件中有这个:
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# this doesn't work on some version of xampp
# RewriteRule ^(.*)$ index.php?rt= [L,QSA]
RewriteRule ^([\s\S]*)$ index.php?rt= [L,B,QSA]
ErrorDocument 404 /error404.html
如果您想要 VirtualHost
中的规则,那么它将是 2 套规则。如果您可以将规则保存在站点根目录 .htaccess 中,那么它将是一个单一的规则。我在这里提供 .htaccess 规则:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
确保删除上面 VirtualHost
中显示的现有规则,并在新浏览器中进行测试以避免旧浏览器缓存。
我已经在我的域上安装了 https。所有这些案例都可以在我的网站上找到:
http://example.com // will be redirected to https://example.com
https://example.com
http://www.example.com // will be redirected to https://www.example.com
https://www.example.com
看到了吗?一切都将使用 https 协议。一切顺利。
现在我需要将所有 www
重定向到 non-www
。所以 http://www.example.com
和 https://www.example.com
应该重定向到 https://example.com
(换句话说,所有情况都应该重定向到这个).
这是我当前在 /etc/apache2/sites-avalible/000-default.conf
中的代码:
<VirtualHost *:80>
.
.
.
RewriteEngine on
RewriteCond %{SERVER_NAME} =lamtakam.com [OR]
RewriteCond %{SERVER_NAME} =www.lamtakam.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] -- this
RewriteRule ^(.*)$ https://%1/ [R=301,L] -- and this are for www redirection
-- but doesn't work
</VirtualHost>
有什么想法吗?
另外 lamtakam 是我所说的确切域,如果您需要检查某些内容。
编辑:
目前我的项目根目录下的 .htaccess
文件中有这个:
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# this doesn't work on some version of xampp
# RewriteRule ^(.*)$ index.php?rt= [L,QSA]
RewriteRule ^([\s\S]*)$ index.php?rt= [L,B,QSA]
ErrorDocument 404 /error404.html
如果您想要 VirtualHost
中的规则,那么它将是 2 套规则。如果您可以将规则保存在站点根目录 .htaccess 中,那么它将是一个单一的规则。我在这里提供 .htaccess 规则:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
确保删除上面 VirtualHost
中显示的现有规则,并在新浏览器中进行测试以避免旧浏览器缓存。