mod_rewrite 用于映射文件中的特定域

发布于 2024-10-09 09:07:41 字数 577 浏览 0 评论 0原文

我有一堆域,我想转到一个域,但该域的各个部分。

# this is what I currently have
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*\.?foo\.com$ [NC]
RewriteRule ^.*$ ${domainmappings:www.foo.com} [L,R=301]

# rewrite map file
www.foo.com www.domain.com/domain/foo.com.php
www.bar.com www.domain.com/domain/bar.com.php
www.baz.com www.domain.com/other/baz.php.foo

问题是我不想让每个域都成为 RewriteCond 的一部分。我尝试过,

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

但这适用于每个域。我只希望映射文件中的域进行重定向,然后如果它与映射文件中的任何域都不匹配,则继续进行其他重写。

I have a bunch of domains that I want to go to one domain but various parts of that domain.

# this is what I currently have
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*\.?foo\.com$ [NC]
RewriteRule ^.*$ ${domainmappings:www.foo.com} [L,R=301]

# rewrite map file
www.foo.com www.domain.com/domain/foo.com.php
www.bar.com www.domain.com/domain/bar.com.php
www.baz.com www.domain.com/other/baz.php.foo

The problem is that I don't want to have to have each domain be part of the RewriteCond. I tried

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

but that will do it for EVERY domain. I only want the domains that are in the mappings file to redirect, and then continue on to other rewrites if it doesn't match any domains in the mappings file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

横笛休吹塞上声 2024-10-16 09:07:41

我很接近,因为我找出了大小写匹配,但无法找出 www 前缀。如果我使用下面的第一个,它无需 www.如果我使用第二个,它可以与 www.如果我同时使用 - 都不起作用。

RewriteCond %{HTTP_HOST} (.*)$ [NC] # works for without www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] # works with www
RewriteCond ${domainmappings:%1|NOTFOUND} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ ${domainmappings:%1} [L,R=301]

有什么想法吗?

I'm close as I figured out the the case matching, but unable to figure out the www prefix. If I use the first one below, it works without www. If I use the second one, it works with the www. If I use BOTH - neither work.

RewriteCond %{HTTP_HOST} (.*)$ [NC] # works for without www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] # works with www
RewriteCond ${domainmappings:%1|NOTFOUND} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ ${domainmappings:%1} [L,R=301]

Any ideas?

牵你手 2024-10-16 09:07:41

你走在正确的轨道上。您所要做的就是使用地图上的管道操作符,这样您就可以掌握所有情况。

RewriteCond ${domainmappings:%{HTTP_HOST}|NOTFOUND}    ^(.+)$
RewriteCond %1  !^NOTFOUND$
RewriteRule ^.*$ ${domainmappings:%1} [L,R=301]

如果主机不在列表中,则第二个条件将不匹配。您仍然需要处理 www 前缀和大小写匹配,但您已经明白了。

You are on the right track. What you have to do is use the pipe operator on the map so that you have a catch-all.

RewriteCond ${domainmappings:%{HTTP_HOST}|NOTFOUND}    ^(.+)$
RewriteCond %1  !^NOTFOUND$
RewriteRule ^.*$ ${domainmappings:%1} [L,R=301]

The second condition will not match if the host is not in the list. You still have to deal with the www prefix, and the case matching, but you get the idea.

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