我已经在我的域上安装了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.exampleSee? 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?
发布评论