Server Bug Fix: Ho to point two different domains to the same web application (Apache)

Original Source Link

I have an app: l’et’s call it Mickey.

To access the app, I’ve configured the vhost in apache, so that when accessing the third level domain mickey.example.com, the app Mickey is served.

This works.

And this is its configuration:

<VirtualHost *:80>
        ServerName mickey.example.com
        # ServerAlias Not required

        ServerAdmin [email protected]
        DocumentRoot /var/www/mickey.example.com/public_html

        ErrorLog /var/www/mickey.example.com/log/error.log
        CustomLog /var/www/mickey.example.com/log/access.log combined

        <Directory /var/www/mickey.example.com>
                AllowOverride All
        </Directory>

        RewriteEngine on
        RewriteCond %{SERVER_NAME} =mickey.example.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Now I want to configure another domain: mickey.example2.com

The end result should be that the Mickey app has to be served when accessed from mickey.example.com and when accessed from mickey.example2.com.

So, basically I have:

  1. The same third level domain
  2. The same app
  3. TWO DIFFERENT second level domains.

WHAT I DID

First attempt: configure a DNS CNAME alias

As first attemp, I simply added a new DNS record in domain example2.com that pointed to mickey.example.com:

DNS for domain example2.com:

  • Type: CNAME
  • Host: Mickey
  • Points to: mickey.example.com

This didn’t worked: after DNS propagation, when I accessed the domain mickey.example.com, the server redirected me to the default domain configured in Apache (that is, let’s say, defaultexample.com)

Second attempt

As I didn’t pointed out immediately that it was the server that redirected to defaultexample.com, I changed the DNS record from a CNAME one to an A one:

DNS for domain example2.com:

  • Type: A
  • Host: Mickey
  • Points to: 123.456.789.012

Where 123.456.789.012 is the IP of the server that serves Mickey app.

This didn’t worked: the server continued to redirect me to defaultexample.com.

At this point I guessed that was the server that redirected me, so I started digging into Vhost configuration (leaving the DNS record as an A one, and it is an A one while I’m writing this question).

Third attempt: ServerAlias in Apache

As first attempt, I tried to add a ServerAlias to the vhost that serves mickey.example.com:

<VirtualHost *:80>
        ServerName mickey.example.com
+       ServerAlias mickey.example2.com

        ServerAdmin [email protected]
        DocumentRoot /var/www/mickey.example.com/public_html

        ErrorLog /var/www/mickey.example.com/log/error.log
        CustomLog /var/www/mickey.example.com/log/access.log combined

        <Directory /var/www/mickey.example.com>
                AllowOverride All
        </Directory>

        RewriteEngine on
        RewriteCond %{SERVER_NAME} =mickey.example.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

This didn’t work: Apache continues to redirect me to defaultdomain.com.

Fourth attempt: A dedicated vhost

At this point I created a dedicated vhost calling it mickey.example2.com.conf:

<VirtualHost *:80>
-        ServerName mickey.example.com
+        ServerName mickey.example2.com
        DocumentRoot /var/www/mickey.example.com/public_html

        ErrorLog /var/www/mickey.example.com/log/error.log
        CustomLog /var/www/mickey.example.com/log/access.log combined

        <Directory /var/www/mickey.example.com>
                AllowOverride All
        </Directory>

        RewriteEngine on
-        RewriteCond %{SERVER_NAME} =mickey.example.com
+        RewriteCond %{SERVER_NAME} =mickey.example2.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Then I enabled the new vhost in Apache and reloaded it:

sudo a2ensite mickey.example2.com.conf
sudo systemctl reload apache2

Nothing: accessing mickey.example2.com continues to redirect me to defaultdomain.com

What to do now?

At this point I don’t know how to proceed: I have tried all what I thought should have worked, but it didn’t.

Any suggestions?

As told, I need to serve the same exact app both from mickey.example.com and from mickey.example2.com.

Any ideas about how to do this?

Finally I solved it!

After many hours of tests and digging in the server, I discovered it was an issue with the Let’s Encrypt digital certificate: after a new challenge and a new verification of the third level domain, now all works as expected! đź’Ş

Tagged :

Leave a Reply

Your email address will not be published. Required fields are marked *