Website owners and developers frequently encounter issues when attempting to redirect exact, full-path URLs or entire subdomains using .htaccess directives. When custom Redirect 301 lines or RewriteRule definitions conflict, requests either land on a 404 page, lose their path parameters, or get trapped in an infinite redirection loop. This guide demonstrates how to configure Apache's mod_rewrite correctly to handle complete URL redirects across subdomains and primary domains.
Quick fix
- Back up your current
.htaccessfile before making changes. - Place rule-based
RewriteRuledirectives above generic WordPress or front-controller routing blocks. - Use
RewriteCondto test the exact domain name (host) andRewriteRuleto target the precise URL path. - Append the
[R=301,L]flags to ensure the browser receives a permanent redirect and stops executing subsequent rules. - Clear your local browser cache or test using an anonymous session to bypass cached HTTP 301 responses.
Why this happens
- Mixing standard
RedirectwithRewriteRule: The standardRedirectdirective (frommod_alias) executes at a different stage thanRewriteRule(frommod_rewrite). Combining them often causes unexpected path concatenation. - Ignoring host conditions: Standard
Redirect 301 /path http://example.com/matches requests across all subdomains attached to that document root unless host checks are explicitly defined viaRewriteCond %{HTTP_HOST}. - Order of execution: Placement below CMS rewrites (such as default WordPress rules) causes Apache to route incoming requests to internal index files before your 301 redirects are evaluated.
- Query string handling: Standard redirect directives silently pass existing query strings (
?id=123) to the target URL unless overridden with a trailing question mark or explicit flags.
How to fix it
1. Redirecting a specific complete URL from a subdomain to the main domain
If you want to map a specific path on a subdomain (e.g., sub.example.com/old-page) to a unique path on your main domain (e.g., example.com/new-page), use mod_rewrite with an explicit host check. Always test configuration changes on your server, and ensure you have backing copies of critical environment files.
Open your .htaccess file and place this block at the very top of the file:
RewriteEngine On
# Redirect a specific URL from sub.example.com to example.com
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^old-page/?$ https://example.com/new-page [R=301,L]
The [NC] flag makes the host check case-insensitive, while [R=301,L] instructs Apache to issue an HTTP 301 status code and halt further rule processing for that request.
2. Redirecting an entire subdomain to the main domain root
To point every request coming to sub.example.com directly to the home page of example.com (discarding the incoming path), capture the domain condition and redirect to the target root:
RewriteEngine On
# Send all requests on sub.example.com to main home page
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^ https://example.com/ [R=301,L]
If you instead want to preserve the request path (so sub.example.com/folder/page becomes example.com/folder/page), use the REQUEST_URI reference:
RewriteEngine On
# Preserve path when moving subdomain to main domain
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
3. Managing a bulk list of exact URL redirects
When migrating dozens of individual URLs from an Excel spreadsheet, standard Redirect 301 lines can fail if the destination domain structure differs or if queries append unwanted paths. Using RewriteRule ensures clean mapping:
RewriteEngine On
# Bulk exact URL mappings
RewriteRule ^category/old-item-1/?$ https://example.com/products/item-1 [R=301,L]
RewriteRule ^category/old-item-2/?$ https://example.com/products/item-2 [R=301,L]
RewriteRule ^about-us-old/?$ https://example.com/about [R=301,L]
For additional details on how rules interact with domain definitions, review the official Apache mod_rewrite documentation.
4. Nginx Configuration Alternative
If your web infrastructure runs on Nginx rather than Apache, .htaccess files are not processed. You must add server blocks inside your Nginx configuration files (such as /etc/nginx/sites-available/example.conf). Modifying server blocks requires sudo administrative privileges.
# Subdomain redirect block in Nginx
server {
listen 80;
listen 443 ssl;
server_name sub.example.com;
# Specific path redirect
location = /old-page {
return 301 https://example.com/new-page;
}
# Catch-all redirect for the rest of the subdomain
location / {
return 301 https://example.com$request_uri;
}
}
Check that it worked
Browsers aggressively cache 301 redirects, which makes testing in standard browser tabs unreliable. Use curl in your local CLI terminal to inspect the exact HTTP response headers returned by your server.
Run the following command to test a specific URL:
curl -I -L http://sub.example.com/old-page
Check the output headers for the expected status line and target location:
HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-page
You can also inspect responses using online header checking tools or the Network tab in browser Developer Tools (ensuring "Disable cache" is checked).
Prevent it next time
- Group custom rewrite rules together at the top of your
.htaccessfile, directly belowRewriteEngine On. - Avoid mixing
mod_aliasdirectives (likeRedirect 301) withmod_rewriterules (likeRewriteRule) in the same file. - Always test redirects using 302 (temporary) status codes during development to prevent your browser from permanently caching invalid rules. Switch to 301 once confirmed working.
- If you manage domain records externally, understand how host routing works by reading about Nameservers vs CNAME Records.
FAQ
Why does my redirect append extra URL paths to the destination?
This happens when using general Redirect directives from mod_alias. Standard Redirect /old http://example.com/new appends any leftover trailing path from the incoming request. Using RewriteRule with strict anchor tags (^ and $) prevents this behavior.
Can I perform 301 redirects at the DNS level?
No. DNS servers only map domain names to IP addresses or other hostnames; they do not process HTTP paths or send HTTP 301 status codes. Redirection requires a web server (like Apache or Nginx) or an edge worker process to return the HTTP location headers.
How do I drop query parameters during a 301 redirect in mod_rewrite?
In modern Apache setups, adding a question mark to the end of the destination URL in a RewriteRule prevents the original query string from being attached. Alternatively, use the QSD (Query String Discard) flag: RewriteRule ^old-path$ https://example.com/new-path [R=301,L,QSD].
301 Redirects on Dhruva Cloud
Dhruva Cloud hosts web applications behind an integrated edge architecture. Every hosted site includes automated SSL certificate issuance and Global CDN routing through Cloudflare. For users creating custom domain routes or subdomains, Dhruva Cloud automatically handles the initial DNS and zone records. You can modify Apache rules directly using the built-in browser File Manager or run tests using the web terminal access included in your dashboard.