Question

Auto redirection vhost2 to vhost1

Im trying to implement a permanent redirection from one virtual host to the second vhost running on the same droplet and Im struggling to get this to work. Im using apache2 and 2 vhosts are setup on the same droplet as per https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-20-04 and both with ssl and resolve on the internet with https://newdomainname and https://olddomainname Ive tried unsuccessfully with mod_rewrite for redirection but cant get my head around how it works It would seem a relatively simple problem as others seem to have had the problem the other way - but any suggestions gratefully received


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hey there! 👋

First, you need to edit the Apache virtual host configuration for olddomainname, eg /etc/apache2/sites-available/olddomainname.conf.

sudo nano /etc/apache2/sites-available/olddomainname.conf

Inside the <VirtualHost> block for olddomainname, add the following lines to handle the redirection:

<VirtualHost *:80>
    ServerName olddomainname
    ServerAlias www.olddomainname
    Redirect permanent / https://newdomainname.com/
</VirtualHost>

For SSL, if you also have an HTTPS version, edit the <VirtualHost *:443> block in olddomainname-le-ssl.conf (or similar) to handle the secure traffic redirection:

<VirtualHost *:443>
    ServerName olddomainname
    ServerAlias www.olddomainname

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/olddomainname/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/olddomainname/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    Redirect permanent / https://newdomainname/
</VirtualHost>

After updating the configuration, restart Apache for the changes to take effect:

sudo systemctl restart apache2

To confirm that the redirection is working as expected, visit https://olddomainname in your browser, and it should automatically redirect to https://newdomainname.

The Redirect permanent directive is the simplest and cleanest way to handle this kind of redirection. It sends a 301 status code, telling search engines and browsers that the old domain has permanently moved to the new one.

Let me know if this works or if you need more help! 🚀

- Bobby

KFSys
Site Moderator
Site Moderator badge
September 22, 2024

To set up a permanent redirection from one virtual host to another using Apache2, you can follow these steps. Since both virtual hosts are configured with SSL, we will focus on redirecting https://olddomainname to https://newdomainname.

Steps to Configure Permanent Redirection:

  1. Ensure mod_rewrite is enabled: Run the following command to ensure the Apache mod_rewrite module is enabled:
sudo a2enmod rewrite
sudo systemctl restart apache2

Configure the Virtual Host for olddomainname: Edit the virtual host configuration file for olddomainname to include a permanent redirect to newdomainname. You can typically find this file in /etc/apache2/sites-available/.

Open the olddomainname configuration:

sudo nano /etc/apache2/sites-available/olddomainname.conf

Inside this configuration, ensure the following is added:

<VirtualHost *:443>
    ServerName olddomainname
    Redirect 301 / https://newdomainname/
    
    # Existing SSL configuration
    SSLEngine on
    SSLCertificateFile /path/to/olddomainname.crt
    SSLCertificateKeyFile /path/to/olddomainname.key
    SSLCertificateChainFile /path/to/chainfile.crt
</VirtualHost>
  • The line Redirect 301 / https://newdomainname/ will permanently redirect all traffic from olddomainname to newdomainname.

  • Ensure SSL and Other Configuration: Make sure that your SSL configurations are still correct in the VirtualHost for olddomainname. You don’t need to modify the newdomainname virtual host, but it should be fully functional.

  • Test Apache Configuration: After saving the file, test your Apache configuration to make sure there are no syntax errors:

sudo apache2ctl configtest

Restart Apache: Once everything is set, restart Apache for the changes to take effect.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more