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
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!
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
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
.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.