.htaccess:仅当没有子目录时才重写
使用此规则:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
您可以将站点中的每个 URL 重定向到开头加上子域 www.
的 URL。
我遇到的问题是它给我带来了其他子域的一些问题,即我不希望 blog.domain.com 也被重定向。
那么有没有办法制定通用规则来避免重定向已经具有子域的任何内容?
提前致谢!!
我希望问题足够清楚!我您需要任何澄清,请询问!
With this rules:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
You can make every URL in the site redirect to the URL plus the subdomain www.
at the beginning.
The problem I am having is that it is giving me some problems with other subdomains, i.e. I wouldn't want blog.domain.com to me redirected too.
So is there any way to do a generic rule to avoid the redirect of anything that already has a subdomain?
Thanks in advance!!
I hope the question is clear enough!! I you need any clarification please ask for it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的 - RewriteCond 的正则表达式匹配“任何不是 www.domain.com 的内容”。如果我理解正确的话,您只想匹配domain.com。
就像这样:
现在如果有人使用domain.com,他们将被重定向。对于包含除domain.com 之外的任何内容的请求,它们将不会被重定向。这包括 www.domain.com 以及 blog.domain.com 以及 dns2.domain.com。
yes - your regex for the RewriteCond matches on "anything that is not www.domain.com". If I understand correctly, you want to match only domain.com.
Like this:
now if someone uses domain.com, they'll be redirected. For requests that have anything other than domain.com, they will not be redirected. This includes www.domain.com as well as blog.domain.com, as well as dns2.domain.com.
感谢您哈哈引导我走向正确的方向。
我设法通过 cPanel 做到这一点,我重定向了所有非 www.带有www。带通配符的子域。
抱歉,不知道可以专门这样做。
谢谢大家的回答和评论!!
Thank you haha for leading me in the right direction.
I managed to do it through cPanel, I redirected all non www. with a www. subdomain with wildcards.
Sorry, didn't know it was possible to do specifically this.
Thank you for all the answers and comments!!