How to Implement Precise Homepage-Only Redirect: shop.test.com → www.test.com/fedex-orders/ with HTTP/HTTPS and Query Parameter Preservation


2 views

When implementing redirects, we often need surgical precision to avoid affecting other parts of the site. A common requirement is redirecting just the homepage while leaving all other URLs untouched. This becomes particularly important when:

  • Migrating specific sections of a website
  • Creating campaign-specific landing pages
  • Maintaining SEO integrity during partial domain changes

For Apache servers using .htaccess, here's the optimal implementation:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^shop\.test\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.test.com/fedex-orders/ [L,R=301]

For those using Nginx, the equivalent would be:


server {
    listen 80;
    server_name shop.test.com;
    
    location = / {
        return 301 http://www.test.com/fedex-orders/;
    }
    
    location / {
        # Normal processing for non-homepage requests
        proxy_pass http://backend;
    }
}

1. Protocol Handling: The above examples use HTTP. For HTTPS sites, simply change the protocol:


RewriteRule ^(.*)$ https://www.test.com/fedex-orders/ [L,R=301]

2. Query Parameter Protection: The solutions automatically preserve query parameters for non-homepage URLs. For example:

  • shop.test.com/?page=blog → Won't redirect
  • shop.test.com/path?query=value → Won't redirect

Verify your redirect works correctly with these test cases:


curl -I http://shop.test.com
# Should return 301 to fedex-orders

curl -I "http://shop.test.com?page=blog"
# Should NOT redirect

curl -I http://shop.test.com/any-path
# Should NOT redirect

These lightweight rewrite rules add minimal overhead because:

  • They only evaluate for the exact homepage path
  • The regex patterns are intentionally simple
  • Non-matching requests bypass the redirect logic entirely

Let me share a common scenario I encountered while working on an e-commerce migration project. We needed to redirect only the root domain (shop.test.com) to a specific subdirectory (www.test.com/fedex-orders), while keeping all other URLs like shop.test.com/?page=blog intact. Here's how we solved it.

For Apache servers, the most elegant solution uses mod_rewrite in your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^shop\.test\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.test.com/fedex-orders/ [R=301,L]

Key points about this implementation:

  • The first condition matches exactly the shop.test.com domain
  • The second condition ensures we only match the root path (/)
  • R=301 performs a permanent redirect (good for SEO)
  • L flag stops further rule processing

For those running Nginx, here's the equivalent server block configuration:

server {
    listen 80;
    server_name shop.test.com;
    
    if ($request_uri = /) {
        return 301 http://www.test.com/fedex-orders/;
    }
    
    # Other configurations...
}

After implementing either solution, verify it works correctly:

  1. shop.test.com → should redirect
  2. shop.test.com/ → should redirect
  3. shop.test.com/?any=param → should NOT redirect
  4. shop.test.com/page → should NOT redirect

From my experience, watch out for these issues:

  • Cache interference - clear your browser cache when testing
  • Multiple redirect rules conflicting with each other
  • Forgetting the trailing slash in the target URL (can cause double redirects)
  • SSL certificates - if using HTTPS, ensure both domains have valid certs

If you need a PHP-based solution (for shared hosting without .htaccess access), here's a simple script:

<?php
if ($_SERVER['HTTP_HOST'] == 'shop.test.com' && $_SERVER['REQUEST_URI'] == '/') {
    header("Location: http://www.test.com/fedex-orders/", true, 301);
    exit;
}
?>

Place this at the top of your index.php file before any output.

When implementing redirects:

  • Apache: The regex-based matching in .htaccess has minimal overhead
  • Nginx: The if condition is evaluated quickly
  • PHP: Adds slight overhead as it runs on every request