Q:

apache rewrite rules, non-www, https

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.

A:

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.

