When migrating sites between servers, we often need to maintain partial accessibility to legacy content. The standard approach using Redirect Permanent
in VirtualHost
works well for complete transitions, but becomes problematic when we need to exclude specific directories.
<VirtualHost IP.AD.DR.ESS:80>
ServerName example.com
Redirect Permanent / https://example.net/
</VirtualHost>
This configuration effectively redirects all traffic from http://example.com to https://example.net. However, we need to preserve access to /specialdir/
on the original server.
The most efficient method is to combine RedirectMatch
with <Location>
directives in the VirtualHost configuration:
<VirtualHost IP.AD.DR.ESS:80>
ServerName example.com
# First, handle the special directory
<Location /specialdir>
# Disable redirect for this specific path
RedirectMatch 404 ^/specialdir/?$
</Location>
# Then redirect everything else
RedirectMatch permanent ^/(.*)$ https://example.net/$1
</VirtualHost>
For more complex exclusion patterns, mod_rewrite provides greater flexibility:
<VirtualHost IP.AD.DR.ESS:80>
ServerName example.com
RewriteEngine On
# Skip redirect for specialdir
RewriteRule ^/specialdir(/.*)?$ - [L]
# Redirect all other requests
RewriteRule ^/(.*)$ https://example.net/$1 [R=301,L]
</VirtualHost>
- The order of directives matters - exclusions must come before the global redirect
- Test with temporary redirects (302) before implementing permanent ones (301)
- Consider adding
ErrorDocument 404
for the excluded directory if needed - Remember to restart Apache after configuration changes
Verify the setup works as expected:
curl -I http://example.com/specialdir/
curl -I http://example.com/any-other-path/
The first should return 200 OK, while the second should show 301 redirect.
When migrating websites to new domains or HTTPS, we often need comprehensive redirects. The standard approach in httpd.conf
looks like:
<VirtualHost IP.AD.DR.ESS:80>
ServerName example.com
Redirect Permanent / https://example.net/
</VirtualHost>
This works beautifully for site-wide redirects, but complications arise when certain directories must remain accessible on the original server.
Apache provides several ways to handle this scenario without resorting to multiple .htaccess
files. The cleanest method uses RedirectMatch
with negative lookahead:
<VirtualHost IP.AD.DR.ESS:80>
ServerName example.com
RedirectMatch Permanent ^/((?!specialdir/).*) https://example.net/$1
</VirtualHost>
For more complex exclusion patterns, we can combine Redirect
with Location
directives:
<VirtualHost IP.AD.DR.ESS:80>
ServerName example.com
<Location />
Redirect Permanent / https://example.net/
</Location>
<Location /specialdir>
# No redirect - serve content normally
</Location>
</VirtualHost>
Remember these technical details when implementing directory exclusions:
- The pattern matching is case-sensitive by default
- Trailing slashes matter in directory paths
- Subdirectories of the excluded path won't automatically inherit the exclusion
Always verify with these commands after making changes:
apachectl configtest
service httpd graceful
For complex scenarios, consider using RewriteCond
with RewriteRule
which offers even more granular control, though it requires mod_rewrite to be enabled.