2

I have a set of requirements for mod_rewrite that is breaking my head. Any hints / pointers would be appreciated:

domains:

www.domain-europe.com
www.domain.com
  1. requests for www.domain-europe.com should be redirected to www.domain.com/europe
  2. requests for www.domain-europe.com/someurl should be redirected to www.domain.com/someurl
  3. any other request coming to the server that is not www.domain-europe.com or www.domain.com should be set to www.domain.com

I can make any 1 and 3 or 2 and 3 of these work, but not all three at the same time.

Here is one of the many iterations I have tried:

RewriteEngine on

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

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

RewriteCond %{HTTP_HOST}   !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.domain.com/$1 [L,R]
Nidal
  • 187
  • 4
  • 11
mdekkers
  • 224
  • 3
  • 8
  • possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted How to Know about Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Jul 29 '14 at 06:16

1 Answers1

1

Your rewrite conditions are only matching on the HTTP host, not on the request uri - and your rewrite rule (.*) matches everything...

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

will match anytime the host is www.domain-europe.com, regardless of whether / is requested, or /foo/bar/virus.exe is requested.

Instead, perhaps try something more like:

RewriteCond %{HTTP_HOST} ^(www\.)?domain-europe\.com$ [NC]
RewriteRule ^/$ http://www.domain.com/europe [R=301,L]
RewriteRule ^/(.+)$ http://www.domain.com$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^/(.*) http://www.domain.com/$1 [L,R]

You might still have to tweak it some. I'm a bit rusty on the exact semantics - but this should at least point you in the right direction.

HTH

Joe Sniderman
  • 2,749
  • 1
  • 21
  • 26
  • Thanks for getting back, I really appreciate the time you took to provide an answer - unfortunately, this didn't work. I spent some time tweaking this, but we will probably resolve this in PHP – mdekkers Jul 29 '14 at 09:01