简体   繁体   English

映射文件中特定域的mod_rewrite

[英]mod_rewrite for specific domains in a mappings file

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. 问题是我不想让每个域都成为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. 我只希望重定向映射文件中的域,然后如果它与映射文件中的任何域都不匹配,则继续进行其他重写。

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. 您仍然必须处理www前缀和大小写匹配,但是您知道了。

I'm close as I figured out the the case matching, but unable to figure out the www prefix. 当我发现大小写匹配时,我很接近,但是无法弄清楚www前缀。 If I use the first one below, it works without www. 如果我使用下面的第一个,则不需要www。 If I use the second one, it works with the www. 如果我使用第二个,则可与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? 有任何想法吗?

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

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