0

I have two applications on the same server and use apache rewrite rules to redirect:

  • www requests to non www
  • http reuests to https

Everything works ok, except one case:
request www.test2.test.eu is redirect to https://www.test1.com content

How can I konfigure it properly?


Rewrites in domain test1.com config file:
ServerName test1.com
RewriteCond %{HTTP_HOST} ^www.test1.com$ [NC]
RewriteRule ^(.*)$ https://www.test1.com/$1 [R=301]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https:// %{SERVER_NAME}%{REQUEST_URI}


Rewrites in domain test2.test.eu config file:
ServerName test2.test.eu
RewriteCond %{HTTP_HOST} ^www.test2.test.eu$ [NC]
RewriteRule ^(.*)$ https://www.test2.test.eu/$1 [R=301]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https:// %{SERVER_NAME}%{REQUEST_URI}

Any suggestions very appreciated.
Kind regards.

gaspar
  • 133
  • 1
  • 10

1 Answers1

1

i think your first vritualhost config is acting as the default for any request on hosts not matching 'test1.com' and 'test2.test.eu'. Try adding this ServerAlias line to see if it gets the request going to the proper config file.

Rewrites in domain test2.test.eu config file:

ServerName test2.test.eu
ServerAlias www.test2.test.eu *.test2.test.eu
RewriteCond %{HTTP_HOST} ^www.test2.test.eu$ [NC]
RewriteRule ^(.*)$ https://www.test2.test.eu/$1 [R=301]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https:// %{SERVER_NAME}%{REQUEST_URI}

This explicitly tells apache that requests to 'www.test2.test.eu' should be handled by this configuration. The second entry on the ServerAlias with asterisk provides a wildcard so that even if the request comes for 'wwww.test2.test.eu' or 'xxx.test2.test.eu', the proper apache config will handle it. With using the wildcard, you could actually leave off the first entry, like this:

ServerName test2.test.eu
ServerAlias *.test2.test.eu
RewriteCond %{HTTP_HOST} ^www.test2.test.eu$ [NC]
RewriteRule ^(.*)$ https://www.test2.test.eu/$1 [R=301]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https:// %{SERVER_NAME}%{REQUEST_URI}  

and it should work the same, although your first rewrite won't catch non-'www' hostnames either way.

user16081-JoeT
  • 1,950
  • 11
  • 18
  • Thanks for your answear. Unfortunately nothing changed when I've added ServerAlias directive as you suggested. Furthermore when I replace my redirect rules by `Redirect permanent / https://test1.com/` and `Redirect permanent / https://test2.test.eu/` - still nothing changed, so everything works ok except `https://www.test2.test.eu` request, wchich is still redirect to `https://www.test1.com` content. Any idea what can causes this problem and how to set it properly? – gaspar Dec 17 '14 at 11:12
  • As you said - apache acts as if default redirect for request `https://www...` was `https://www.test1.com`. Question is how to change it? – gaspar Dec 17 '14 at 11:36
  • 1
    i'm wondering what your httpd-ssl config looks like, perhaps it's the same situation and you need to define the two hosts on separate ip addresses – user16081-JoeT Dec 17 '14 at 15:33
  • And bingo! :) that was the cause of my problem. I've defined virtualhosts on separate ips and now everything works as it should. Thank you very much for your help! :) – gaspar Dec 17 '14 at 20:14
  • that's great, you're welcome! don't forget to mark this answer as correct - thanks! – user16081-JoeT Dec 17 '14 at 20:30