用 mod_rewrite 替换部分 URL

发布于 2024-09-19 08:38:47 字数 328 浏览 1 评论 0原文

我需要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后eg是否自 2024-09-26 08:38:48

尝试这些规则:

RewriteCond %{ENV:DOMAIN_DIR} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%2]

RewriteCond %{ENV:DOMAIN_DIR} ^([^.]*)\.(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%1_%2,N]

RewriteCond %{ENV:DOMAIN_DIR} ^[^.]+$
RewriteRule ^images/.+ %{ENV:DOMAIN_DIR}/$0 [L]

第一个规则将获取主机并将其存储在环境变量 DOMAIN_DIR 中,不带 www.。第二条规则将一次替换一个点; N 标志允许重新启动重写过程而不增加内部递归计数器。最后第三条规则会将请求重写到对应的目录。

Try these rules:

RewriteCond %{ENV:DOMAIN_DIR} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%2]

RewriteCond %{ENV:DOMAIN_DIR} ^([^.]*)\.(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%1_%2,N]

RewriteCond %{ENV:DOMAIN_DIR} ^[^.]+$
RewriteRule ^images/.+ %{ENV:DOMAIN_DIR}/$0 [L]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文