9

I have a rewritemap that has a list of domains to redirect. Currently I have to list www.foo.com and foo.com in the rewrite map. I was wondering if there was a way to have the rewritecond check for both www and non-www in 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 tried doing things like (www.)%{HTTP_HOST} or ^(www.)%{HTTP_HOST} but no luck.

Scott Foster
  • 465
  • 3
  • 9
  • 17

4 Answers4

18

This should do it:

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

The first RewriteCond will remove the optional www. prefix. The remainder is then used as parameter for the rewrite map in the second RewriteCond.

A plain text file rewrite map returns an empty string if no match is found:

If the key is found, the map-function construct is substituted by SubstValue. If the key is not found then it is substituted by DefaultValue or by the empty string if no DefaultValue was specified.

So if the second condition is fulfilled (note the ^(.+)$), a match has been found and %1 will contain the SubstValue (in this case file.php).

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • that doesn't work because i would still have to have entries for www.foo.com and foo.com. I want to only have an entry for foo.com – Scott Foster Dec 30 '10 at 22:58
  • Almost there. Both www.foo.com and foo.com go to the right place, but the browser just keeps redirecting until the browser quits. Any idea why? – Scott Foster Dec 30 '10 at 23:05
  • ack, got it. It was just going to /page.php instead of domain.com/page.php. I owe you a beer ;) – Scott Foster Dec 30 '10 at 23:08
8

From a post 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]
Nikki9696
  • 6,260
  • 1
  • 28
  • 23
0
RewriteCond %{HTTPS} off

RewriteCond %{HTTP_HOST} ^domain.com$ [OR]

RewriteCond %{HTTP_HOST} ^www.domain.com$

RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
ANIS.A
  • 1
  • Quote: _I was wondering if there was a way to have the rewritecond check for both www and non-www in the same line._ – Matt Clark Apr 22 '16 at 03:44
0

You can try to make the www. part optional with 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 would first redirect anything.anything to www.anything.anything and then apply your rule on the next request. Not too knowledgeable with rewrite maps though so no guarantees.

Seldaek
  • 40,986
  • 9
  • 97
  • 77