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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hey there! 👋
First, you need to edit the Apache virtual host configuration for
olddomainname
, eg/etc/apache2/sites-available/olddomainname.conf
.Inside the
<VirtualHost>
block forolddomainname
, add the following lines to handle the redirection:For SSL, if you also have an HTTPS version, edit the
<VirtualHost *:443>
block inolddomainname-le-ssl.conf
(or similar) to handle the secure traffic redirection:After updating the configuration, restart Apache for the changes to take effect:
To confirm that the redirection is working as expected, visit
https://olddomainname
in your browser, and it should automatically redirect tohttps://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
tohttps://newdomainname
.Steps to Configure Permanent Redirection:
mod_rewrite
is enabled: Run the following command to ensure the Apachemod_rewrite
module is enabled:Configure the Virtual Host for
olddomainname
: Edit the virtual host configuration file forolddomainname
to include a permanent redirect tonewdomainname
. You can typically find this file in/etc/apache2/sites-available/
.Open the
olddomainname
configuration:Inside this configuration, ensure the following is added:
The line
Redirect 301 / https://newdomainname/
will permanently redirect all traffic fromolddomainname
tonewdomainname
.Ensure SSL and Other Configuration: Make sure that your SSL configurations are still correct in the
VirtualHost
forolddomainname
. You don’t need to modify thenewdomainname
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:
Restart Apache: Once everything is set, restart Apache for the changes to take effect.