Apache2 URL Redirection: How to Redirect Domain Root to a Subdirectory (e.g., /git/)


5 views

When configuring Apache2 on Debian (or any Linux distribution), you might need to redirect traffic from a domain's root (e.g., http://git.example.com) to a specific subdirectory (e.g., http://git.example.com/git/). This is common when hosting web applications or version control systems like GitWeb that reside in subdirectories.

The simplest method is using Apache's Redirect directive in your virtual host configuration:


<VirtualHost *:80>
    ServerName git.example.com
    Redirect / http://git.example.com/git/
</VirtualHost>

For more control, use mod_rewrite:


<VirtualHost *:80>
    ServerName git.example.com
    
    RewriteEngine On
    RewriteRule ^/$ /git/ [R=301,L]
</VirtualHost>

The R=301 performs a permanent redirect, while L stops further processing of rules.

If you prefer the content to be served from /git/ without changing the URL:


<VirtualHost *:80>
    ServerName git.example.com
    DocumentRoot /var/www/git.example.com/public_html
    
    Alias / /var/www/git.example.com/public_html/git/
</VirtualHost>
  • Ensure mod_alias is enabled for Redirect/Alias methods: sudo a2enmod alias
  • For mod_rewrite: sudo a2enmod rewrite
  • After changes: sudo systemctl reload apache2

Use curl to verify the redirect:


curl -I http://git.example.com

Should return HTTP 301 or 302 status with Location header pointing to /git/


When managing multiple virtual hosts on Apache2 (Debian/Ubuntu systems), you might need to redirect a bare domain like http://git.example.com to a subdirectory such as http://git.example.com/git/. This is common when hosting web applications in subdirectories while maintaining clean URLs.

Basic Redirect directives often create infinite loops or don't preserve the original domain. The key is to use either:

  • mod_alias Redirect - Simple but less flexible
  • mod_rewrite Rules - More powerful for complex cases

Add this to your VirtualHost configuration:


<VirtualHost *:80>
    ServerName git.example.com
    Redirect permanent / http://git.example.com/git/
</VirtualHost>

Note: This creates a 301 redirect which browsers and search engines will cache.

For more control, use this in your VirtualHost or .htaccess:


<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/git/
    RewriteRule ^(.*)$ /git/$1 [L]
</IfModule>

Advantages:

  • Preserves original request paths
  • Excludes existing /git/ accesses from redirection
  • Works with POST data and complex URLs

1. Trailing Slash Issues: Always test with and without trailing slashes
2. SSL Certificates: Ensure your certificate covers both root and subdirectory URLs
3. Relative Paths: Website assets might break - use absolute paths in your HTML


# Check syntax
sudo apache2ctl configtest

# For mod_rewrite debugging
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Modify the RewriteRule to handle query parameters:


RewriteRule ^(.*)$ /git/$1 [L,QSA]

The QSA flag (Query String Append) merges existing and new parameters.