Debugging Apache 301 Redirects: Tracing Permanent Redirects in Virtual Host Configuration


2 views

When working with Apache virtual hosts on Windows, encountering unexpected 301 redirects can be particularly frustrating. The server seems to make decisions without any visible configuration, especially when all standard locations (.htaccess, httpd.conf) show no signs of the redirect rule.

Beyond the obvious locations, these are often overlooked sources of redirects:

  • Main Apache configuration (httpd.conf)
  • Virtual host configuration files
  • Global redirect modules (mod_alias, mod_rewrite)
  • PHP header() calls in application code
  • Windows hosts file interference

Enable Apache's most verbose logging to track redirect behavior:

LogLevel debug
RewriteLog "C:/path/to/rewrite.log"
RewriteLogLevel 9

For virtual hosts specifically:


    ServerName local.dev
    DocumentRoot "C:/path/to/htdocs"
    LogLevel debug
    ErrorLog "C:/logs/local.dev-error.log"
    CustomLog "C:/logs/local.dev-access.log" combined

Search configurations for these patterns:

# Simple Redirect
Redirect permanent /old http://newdomain.com

# mod_rewrite rule
RewriteEngine On
RewriteRule ^(.*)$ http://external.com/$1 [R=301,L]

# PHP redirect
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://external.com");

On Windows systems, pay special attention to:

  • Case sensitivity in paths
  • File permission issues
  • Multiple Apache instances running
  • Conflicts with other web servers (IIS, XAMPP)

Apache's built-in configuration test can reveal loading order:

httpd.exe -S
httpd.exe -M
httpd.exe -t -D DUMP_MODULES

Before concluding it's Apache, rule out these network factors:

# Check hosts file
C:\Windows\System32\drivers\etc\hosts

# Verify DNS
nslookup yourdomain.com

# Clear browser cache completely

When dealing with unexpected 301 redirects in Apache, the first step is to systematically examine all possible configuration sources. Here's what I've learned from troubleshooting similar issues:

# Example of what you might see in Fiddler
HTTP/1.1 301 Moved Permanently
Location: https://unwanted-site.com
Server: Apache/2.4.41 (Win64)

These are the key locations where redirects can originate:

1. Main Apache config (httpd.conf or apache2.conf)
2. Virtual host configuration files
3. .htaccess files in document root and parent directories
4. Included configuration files
5. Module-specific configs (mod_rewrite, mod_alias)

Here are powerful methods to track down the redirect source:

# 1. Enable rewrite logging (add to httpd.conf)
RewriteLog "logs/rewrite.log"
RewriteLogLevel 3

# 2. Check all loaded modules
apachectl -M | grep -i rewrite

# 3. Search entire config tree
grep -r "Redirect" /etc/apache2/
grep -r "RewriteRule" /etc/apache2/

On Windows systems, pay special attention to:

  • Case sensitivity in paths
  • Directory permissions
  • Configuration file encoding (CRLF vs LF)
  • Windows-style paths in configurations

Let's examine a real-world debugging session:

# First, verify which config files are loaded
httpd -S

# Search for redirect directives
findstr /s /i "redirect" *.conf
findstr /s /i "rewriterule" *.conf

# Check virtual host inheritance
httpd -D DUMP_VHOSTS

These patterns often cause unexpected redirects:

# 1. Domain canonicalization
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# 2. HTTPS enforcement
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# 3. Old URL migration
Redirect 301 /old-path https://new-site.com/new-path

For complex rewrite rules, enable detailed logging:

# In your VirtualHost configuration
LogLevel alert rewrite:trace6
ErrorLog "logs/rewrite_debug.log"