简体   繁体   中英

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. 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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