用 mod_rewrite 替换部分 URL
我需要一个 mod_rewrite 规则来根据它们来自的主机名来重定向 url。
情况:
我们有多个域指向同一个网络空间,我们需要限制特定主机可以查看/下载的内容。
domainname.com/images/logo.jpg 和 /www.domainname.com/images/logo.jpg 应该转换为 domainname.com/domainname_com/images/logo.jpg
所以基本上我需要一个规则/函数来替换%{HTTP_HOST} 与 _ 并删除/替换 www 子域。
有没有办法用 mod_rewrite 来做到这一点?
I need a mod_rewrite rule to redirect url depending on the hostname they are comming from.
The situation:
We have multiple domains pointing to a same webspace and we need to restrict what the specific host can see/download.
domainname.com/images/logo.jpg and /www.domainname.com/images/logo.jpg should transform into domainname.com/domainname_com/images/logo.jpg
So basically I need a rule/function that replaces the dots in the %{HTTP_HOST} with _ and removes/replaces the www subdomain.
Is there any way to do this with mod_rewrite?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这些规则:
第一个规则将获取主机并将其存储在环境变量 DOMAIN_DIR 中,不带
www.
。第二条规则将一次替换一个点; N 标志允许重新启动重写过程而不增加内部递归计数器。最后第三条规则会将请求重写到对应的目录。Try these rules:
The first rule will take the host and store it without
www.
in the environment variable DOMAIN_DIR. The second rule will replace one dot at a time; the N flag allows to restart the rewriting process without incrementing the internal recursion counter. Finally, the third rule will rewrite the request to the corresponding directory.