简体   繁体   English

Apache:如果主机不是以www或CDN开头的则重定向

[英]Apache: redirect if host doesn't start with www or cdn

My goal is to redirect to domain with www. 我的目标是使用www.重定向到域www. if both these conditions are met: 如果同时满足以下两个条件:

  1. current host is not www.example.com 当前主机不是www.example.com
  2. current host is not cdn.example.com 当前主机不是cdn.example.com

I'm using a .htaccess file with this code, but the second condition is not honoured: 我正在使用带有此代码的.htaccess文件,但不满足第二个条件:

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

Thank you 谢谢

Using this did the trick: 使用此技巧:

RewriteCond %{HTTP_HOST} !^(cdn|www)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

I already tried it before but was tricked --by a cached redirect-- to think it was not working. 我之前已经尝试过,但是被缓存的重定向欺骗了,以为它不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM