I've got a rewritemap which has a listing of domain names to redirect. Presently I must list www.foo.com and foo.com within the rewrite map. I'm wondering if there is a method to possess the rewritecond look for both www and non-www within the same line.

# Rewrite Map
foo.com file.php
www.foo.com file.php

# modrewrite
RewriteCond ${domainmappings:%{HTTP_HOST}} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ www.domain.com/%1 [L,R=301]

I attempted doing such things as (www.)% or ^(www.)% but no luck.

From the publish here

http://www.eukhost.com/forums/f15/simple-rewriterule-set-redirect-domain-6570/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^xyz.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.xyz.com$
RewriteRule ^(.*)$ http://www.xyz.com/test//$1 [R=301,L]

This will get it done:

RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteCond ${domainmappings:%2} ^(.+)$ [NC]
RewriteRule ^ /%1 [L,R=301]

The very first RewriteCond will take away the optional www. prefix. The remaining will be used as parameter for that rewrite map within the second RewriteCond.

An ordinary text file rewrite map returns a clear string if no match is located:

If the bottom line is found, the map-function construct is replaced by SubstValue. If the bottom line is not found then its replaced by DefaultValue or through the empty string if no DefaultValue was specified.

Therefore if the 2nd condition is satisfied (note the ^(.+)$), a match has been discovered and %1 will retain the SubstValue (within this situation file.php).

You can test to create the www. part optional using the following:

# Rewrite Map
www.foo.com file.php

# modrewrite

# redirect to www domain always
RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)$
RewriteRule (.*) http://www\.%1/$1 [L,R=301,QSA)

# redirect following the map
RewriteCond ${domainmappings:%{HTTP_HOST}} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ www.domain.com/%1 [L,R=301]

This could first redirect anything.anything to www.anything.anything after which apply your rule around the next request. Much less knowledgeable with rewrite maps though so no guarantees.