0

I have two applications configured on one apache (eg. example.com and test.net). Rewrite rules for those apps are the same (so in config file for domain example.com I've got this rules, and the same rules in conf file for test.net domain)

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

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

And it almost works ok, except for one case - when I request in webwrowser https://www.test.net.
In this only case apache redirets to https://www.test.net but displays content of https://www.example.com (not content of https://www.test.net as it should). Request www.test.net works ok. And https:// www.example.com also works ok.

And here's whole Virtual Host config for test.net (config for example.com is analogical):

<VirtualHost *:80>
    ServerName test.net

    DocumentRoot /var/www/test/web/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/test/web/>
        Options FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/test_error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/test_access.log combined

    RewriteEngine on  

    RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

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


</VirtualHost>

And Virtual Host file for example.com

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com

        DocumentRoot /var/www/example/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/example/>
                Options FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/example_error.log
        LogLevel warn   
        CustomLog ${APACHE_LOG_DIR}/example_access.log combined

        RewriteEngine on

        RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
        RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

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

</VirtualHost>

I've got also ofcourse config file for ssl settings (eg. ssl certs), but there's no redirect rules.

How to configure rewrite rules for this two applications so that it work properly?
Any suggestions very appreciated! So far I don't know where my mistake is.

Thanks in advance,
Kind regards!

gaspar
  • 133
  • 1
  • 10

1 Answers1

0

I think you are missing ServerAlias www.test.com and ServerAlias www.example.com in your VirtualHost configs and that causes you these problems.

Glueon
  • 3,514
  • 2
  • 22
  • 31
  • I've just added ServerAlias definition to my config files, but unfortunately it hasn't change a thing - apache still redirects my request https :// www .test.net to https :// www .example.com content. But thanks for suggestion! Still searching for the answer. – gaspar Oct 16 '14 at 13:32
  • Another interesting thing is `RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}` I think you meant `RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}` – Glueon Oct 16 '14 at 13:44
  • No I meant `RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}`. But meanwhile I've checked your version with HTTP_HOST and nothings changed (finally redirect is like you've posted, so: `RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}`) – gaspar Oct 16 '14 at 13:50
  • It looks like every request contains www. (so https :// www.test.net too) is caught by the application example.com config (?), because https :// www.test.net redirets to https :// www.test.net but displays content of example.com application – gaspar Oct 16 '14 at 19:37
  • I've just added virtual host config file for example.com to my question, maybe it would help to get full picture. – gaspar Oct 17 '14 at 07:03