Here's how you can do it:
- SSH into your Ubuntu server.
- Open the Apache configuration file for your website. The file is usually located in the
/etc/apache2/sites-available/
directory and has a.conf
extension. For example, if your website is called "example.com", the configuration file may be namedexample.com.conf
. - Edit the configuration file using a text editor like
nano
orvi
. For example:shellsudo nano /etc/apache2/sites-available/example.com.conf
- Look for the
<VirtualHost>
section for your website. It typically starts with<VirtualHost *:80>
or<VirtualHost *:443>
for HTTP and HTTPS respectively. - Within the
<VirtualHost>
section, add the following lines to redirect all requests to the "www" version of your website:apache<VirtualHost *:80> ServerName example.com
ServerAlias *.example.com
Redirect permanent / http://www.example.com/ </VirtualHost>
example.com
with your actual domain name. - Save the configuration file and exit the text editor.
- Enable the new configuration by creating a symbolic link to the
sites-enabled
directory:shellsudo a2ensite example.com.conf
example.com.conf
with the actual name of your configuration file. - Restart Apache for the changes to take effect:
shell
sudo service apache2 restart
Now, when you access your website without the "www" prefix, it should automatically redirect to the "www" version, where your Django application is hosted.
TAGS