How to Redirect Your Root Domain to a Specific Subdirectory in Apache

When hosting multiple projects on a single Apache server, you may want to redirect visitors from your root domain to a specific subdirectory while keeping other projects intact. In this guide, we’ll walk you through the steps to:

✅ Prevent direct access to the server’s root directory
✅ Redirect all traffic from example.com to example.com/blog
✅ Ensure redirection works over both HTTP and HTTPS

1. Understanding the Directory Structure

Let’s say our server’s web directory is located at:

/var/www/html/

Inside this directory, we have multiple projects:

/var/www/html/blog # WordPress or any main site /var/www/html/app1 # Another application /var/www/html/app2 # Another project

We want anyone visiting example.com or 192.168.1.100 (the server’s public IP) to be automatically redirected to example.com/blog, while keeping other projects untouched.


2. Prevent Direct Access to /var/www/html

To prevent directory listing or direct access to /var/www/html, create or edit the .htaccess file in /var/www/html/:

nano /var/www/html/.htaccess

Add the following rules:

# Disable directory listing
Options -Indexes

# Redirect root domain to /blog
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /blog/ [R=301,L]

This ensures:
✅ No one can list the contents of /var/www/html
✅ Visitors accessing example.com or 192.168.1.100 will be redirected to /blog/


3. Configure Apache for HTTP Redirection

Now, let’s modify Apache’s configuration for HTTP (port 80) requests.

Edit the default virtual host file

nano /etc/apache2/sites-available/000-default.conf

Replace the contents with:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com 192.168.1.100

    DocumentRoot /var/www/html

    # Redirect root domain to /blog
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^$ /blog/ [R=301,L]

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

4. Configure Apache for HTTPS Redirection

If you have an SSL certificate installed, you must also configure the HTTPS (port 443) settings.

Edit your SSL virtual host configuration

nano /etc/apache2/sites-available/example-ssl.conf

Modify the <VirtualHost *:443> block as follows:

<VirtualHost *:443>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    # Redirect root domain to /blog
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^$ /blog/ [R=301,L]

    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This ensures that both HTTP and HTTPS traffic are redirected correctly.


5. Enable Apache Modules & Restart Apache

Make sure mod_rewrite is enabled:

a2enmod rewrite
systemctl restart apache2

Now, restart Apache to apply the changes:

systemctl restart apache2

6. Verify the Redirection

After applying the changes, visit the following:

http://example.com → Redirects to http://example.com/blog
https://example.com → Redirects to https://example.com/blog
http://192.168.1.100 → Redirects to http://192.168.1.100/blog
https://192.168.1.100 → Redirects to https://192.168.1.100/blog


7. Clear Cache if Redirection Doesn’t Work

If the redirection doesn’t take effect immediately, try:

systemctl restart apache2

Final Thoughts

With this setup, your visitors will always land on example.com/blog when accessing your root domain while keeping other projects in /var/www/html untouched.

error: Content is protected !!