如何将www重定向到非www https?

编程入门 行业动态 更新时间:2024-10-19 07:35:28
本文介绍了如何将www重定向到非www https?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在我的域上安装了https.所有这些情况都可以在我的网站上找到:

example // will be redirected to example example www.example // will be redirected to www.example www.example

看到了吗?一切都将使用https协议.很好.

现在,我需要将所有www重定向到non-www.因此,应将www.example和www.example重定向到example (换句话说,所有情况都应重定向到此).

这是我在/etc/apache2/sites-avalible/000-default.conf中的当前代码:

<VirtualHost *:80> . . . RewriteEngine on RewriteCond %{SERVER_NAME} =lamtakam [OR] RewriteCond %{SERVER_NAME} =www.lamtakam RewriteRule ^ %{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] -- this RewriteRule ^(.*)$ %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 ^ %1%{REQUEST_URI} [R=301,L,NE]

确保删除上面VirtualHost中显示的现有规则,并在新的浏览器中进行测试,以避免旧的浏览器缓存.

I have installed https on my domain. And all this cases are available of my website:

example // will be redirected to example example www.example // will be redirected to www.example www.example

See? everything will be using https protocol. All fine.

Now I need to redirect all www to non-www. So www.example and www.example should be redirected to example (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 [OR] RewriteCond %{SERVER_NAME} =www.lamtakam RewriteRule ^ %{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] -- this RewriteRule ^(.*)$ %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 ^ %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?

本文发布于:2023-10-31 13:16:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   重定向   www   https

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!