How to Redirect Specific URL Paths to Another Domain in Nginx Without Double Slashes


2 views

When implementing domain redirection in Nginx, a common pitfall occurs with URL path concatenation. The original attempt using:

server {
  listen 1.2.3.4:80;
  server_name "example.com";
  rewrite ^(.*) http://answares.com/examples_com$1 permanent;
}

Produces undesirable results because of how Nginx handles URI concatenation. The double slash issue (examples_com//something) stems from automatic slash handling in the rewrite directive.

Here's the correct implementation that maintains clean URLs:

server {
    listen 1.2.3.4:80;
    server_name example.com;
    
    location /something {
        return 301 http://answares.com/examples_com$request_uri;
    }
    
    # For redirecting all paths under a specific prefix
    location ~ ^/(.*)$ {
        return 301 http://answares.com/examples_com/$1;
    }
}

For more complex scenarios, consider these variations:

# Method 1: Using rewrite with capture groups
server {
    listen 80;
    server_name example.com;
    rewrite ^/(.*)$ http://answares.com/examples_com/$1 permanent;
}

# Method 2: Using regex matching for specific paths
server {
    listen 80;
    server_name example.com;
    
    if ($request_uri ~* "^/something(.*)$") {
        return 301 http://answares.com/examples_com$1;
    }
}

After implementing the changes:

  1. Test with curl -I http://example.com/something to verify the Location header
  2. Check Nginx error logs for any rewrite issues
  3. Verify the final URL doesn't contain double slashes

The return directive is generally more efficient than rewrite for simple redirects. For high-traffic sites:

  • Use 301 (permanent) redirects for SEO-friendly permanent moves
  • Consider implementing these rules at the server block level rather than location blocks when possible
  • Test redirect chains to ensure no unnecessary hops

When working with Nginx redirects, a common requirement is to map requests from one domain to a specific path on another domain while preserving the URI components. The key challenge lies in maintaining clean URL structures without duplicate slashes or missing path segments.

For the specific case of redirecting http://example.com/something to http://answares.com/examples_com/something, here's the most robust configuration:

server {
    listen 80;
    server_name example.com;
    
    location / {
        return 301 http://answares.com/examples_com$request_uri;
    }
}

Using $request_uri instead of regex capture groups provides several advantages:

  • Preserves the complete original URI including query strings
  • Automatically handles URL encoding properly
  • Maintains exactly one slash between domains and paths

If you need more complex pattern matching, you can use rewrite with careful handling:

server {
    listen 80;
    server_name example.com;
    
    rewrite ^/(.*)$ http://answares.com/examples_com/$1 permanent;
}

After implementing the redirect, verify it works correctly for these cases:

curl -I http://example.com/
curl -I http://example.com/something
curl -I http://example.com/something?param=value

For high-traffic sites, consider these optimizations:

  • Use return 301 instead of rewrite ... permanent for better performance
  • Ensure keepalive connections are properly configured
  • Set appropriate cache headers for the redirect responses