When migrating application resources from a folder structure (www.example.com/dir
) to a dedicated subdomain (dir.example.com
), proper redirection becomes crucial for both user experience and SEO. The key requirement is to maintain URL consistency while ensuring all requests to the old path get properly forwarded to the new subdomain.
Apache's mod_rewrite provides the most robust solution for this redirection pattern. Create or modify the .htaccess
file in the /dir
directory with the following content:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^dir(?:/(.*))?$ https://dir.example.com/$1 [L,R=301]
This configuration works by:
- Activating the rewrite engine
- Checking if the request comes from either www.example.com or example.com
- Capturing any path segments after /dir for proper redirection
- Issuing a 301 (permanent) redirect to preserve SEO value
For more complex scenarios, consider these variations:
# Redirect specific file types only
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^dir/(.*\.(php|html))$ https://dir.example.com/$1 [L,R=301]
# Redirect with query parameters preserved
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^dir(?:/(.*))?$ https://dir.example.com/$1?%1 [L,R=301]
After implementation, verify the redirection works correctly:
- Test various URL patterns (
/dir
,/dir/file.ext
,/dir/sub/path
) - Check that query parameters are preserved (if needed)
- Confirm the HTTP status code is 301 via browser developer tools
Developers often encounter these issues:
- Redirect loops when the .htaccess is placed in both root and subdirectory
- Broken links when not preserving the URI path segments
- SSL certificate warnings if the subdomain isn't properly configured
For high-traffic sites, evaluate these optimizations:
- Place redirect rules in the main server config instead of .htaccess
- Use
RewriteBase
directives when dealing with complex path structures - Consider implementing redirects at the load balancer level if available
When migrating application assets from a directory structure (www.example.com/dir
) to a dedicated subdomain (dir.example.com
), proper redirection is crucial for both user experience and SEO. The files remain physically in the same server location, but we need to change how users access them.
Create or modify the .htaccess
file in your target directory (/dir
) with these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^dir(/(.*))?$ https://dir.example.com/$2 [R=301,L]
RewriteCond checks if the request is coming to the main domain (with or without www)
RewriteRule matches the /dir path and captures any additional path segments
R=301 indicates a permanent redirect (better for SEO)
L flag makes this the last rule to process
If you prefer mod_alias syntax (though less flexible):
Redirect 301 /dir https://dir.example.com
After implementing:
- Clear your browser cache
- Use curl to test:
curl -I https://www.example.com/dir
- Verify the response shows 301 status and correct Location header
- Forgetting to enable mod_rewrite (
a2enmod rewrite
on Debian-based systems) - Not accounting for HTTPS in the redirect target
- Overlooking existing redirect rules that might interfere
To maintain URL parameters during redirection:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^dir(/(.*))?$ https://dir.example.com/$2 [R=301,L,QSA]
The QSA flag appends any query string from the original request.