如何将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/$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=$1 [L,QSA]
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [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
中显示的现有规则,并在新的浏览器中进行测试,以避免旧的浏览器缓存.
I have installed https on my domain. And all this cases are available of my website:
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
See? everything will be using https protocol. All fine.
Now I need to redirect all www
to non-www
. So http://www.example.com
and https://www.example.com
should be redirected to https://example.com
(in other word, all cases should be redirected to this).
Here is my current code in the /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/$1 [R=301,L] -- and this are for www redirection
-- but doesn't work
</VirtualHost>
any idea?
also lamtakam is my exact domain I was talking about, if you need to check something.
Edit:
Currently I have this inside .htaccess
file on the root of my project:
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=$1 [L,QSA]
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
ErrorDocument 404 /error404.html
If you want rules in VirtualHost
then it would be 2 set of rules. If you can keep rules in site root .htaccess then it would be a single rule. I am providing .htaccess rule here:
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]
Make sure you remove your existing rules shown in VirtualHost
above and you test in a new browser to avoid old browser cache.
这篇关于如何将www重定向到非www https?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!