Handling Bare URLs on Apache

Here's how you can do it:

  1. SSH into your Ubuntu server.
  2. 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 named example.com.conf.
  3. Edit the configuration file using a text editor like nano or vi. For example:
    sudo nano /etc/apache2/sites-available/example.com.conf
  4. Look for the <VirtualHost> section for your website. It typically starts with <VirtualHost *:80> or <VirtualHost *:443> for HTTP and HTTPS respectively.
  5. Within the <VirtualHost> section, add the following lines to redirect all requests to the "www" version of your website:
    <VirtualHost *:80> ServerName example.com
    ServerAlias *.example.com
    Redirect permanent / http://www.example.com/ </VirtualHost>
    Make sure to replace example.com with your actual domain name.
  6. Save the configuration file and exit the text editor.
  7. Enable the new configuration by creating a symbolic link to the sites-enabled directory:
    sudo a2ensite example.com.conf
    Replace example.com.conf with the actual name of your configuration file.
  8. Restart Apache for the changes to take effect:
    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.


Ubuntu apache hosting