Apache vs Nginx Redirects: Configuration Examples for Common URL Rules
ApacheNginxhtaccessweb serversdeveloper guide

Apache vs Nginx Redirects: Configuration Examples for Common URL Rules

RRedirect Portal Editorial Team
2026-11-17
7 min read

Compare Apache and Nginx redirect syntax with practical rules for URL moves, HTTPS, www, regex patterns, query strings and safe testing.

Apache and Nginx can both deliver reliable, SEO-safe URL redirects, but their configuration syntax and rule processing are different. This guide gives you reusable examples for single URLs, folder moves, regular expressions, HTTPS enforcement, www canonicalisation and query-string handling, followed by a deployment checklist for testing rules safely.

Overview

An Apache redirect is commonly written in a .htaccess file or a virtual-host configuration. Apache supports several redirect approaches, including the simple Redirect directive from mod_alias and the more flexible RewriteRule syntax from mod_rewrite. A basic Nginx redirect is usually declared inside a server block using return or rewrite.

For a permanent URL move, use a 301 response when the old address should be replaced by the new address. For a genuinely temporary routing change, a 302 may be more appropriate. The choice is about the intended behaviour, not the server software. See our guide to 301 vs 302 redirects before choosing the status code.

TaskApacheNginx
Typical location.htaccess or virtual-host configurationserver block configuration
Simple redirectRedirect or RewriteRulereturn
Pattern matchingRewriteRule with regular expressionsrewrite with regular expressions or a carefully designed location
Configuration testapachectl configtest or an equivalent service commandnginx -t
Common riskRule order, per-directory paths and unsupported modulesServer-block selection, location precedence and regex capture mistakes

Always confirm which web server actually handles the request. A hosting panel may expose Apache settings while a reverse proxy, CDN or Nginx layer processes traffic first. A rule placed in the wrong layer can appear valid but never run.

Checklist by scenario

1. Redirect one URL

For Apache, a simple Redirect is often easier to review than a regular expression:

Redirect 301 /old-page/ https://www.example.co.uk/new-page/

In an Apache .htaccess file, the path is normally relative to the site root. If you use mod_rewrite, the equivalent is:

RewriteEngine On
RewriteRule ^old-page/?$ https://www.example.co.uk/new-page/ [R=301,L]

For Nginx, put the rule in the relevant server block:

location = /old-page/ {
    return 301 https://www.example.co.uk/new-page/;
}

An exact Nginx location helps prevent a similarly named path from matching accidentally. If the old URL may or may not include a trailing slash, test both forms or define a deliberate slash policy.

2. Move a folder while preserving the rest of the path

Apache's Redirect directive can preserve the suffix after the matched prefix:

Redirect 301 /old-section/ https://www.example.co.uk/new-section/

For a regular-expression approach in Apache:

RewriteRule ^old-section/(.*)$ https://www.example.co.uk/new-section/$1 [R=301,L]

The corresponding Nginx rule is:

rewrite ^/old-section/(.*)$ /new-section/$1 permanent;

Here, permanent produces a 301 response. Test the base folder, nested pages, URL-encoded characters and any paths that should be excluded. Do not assume that a broad folder rule is safe if the destination structure is not identical.

3. Apply a regex rule carefully

Regular expressions are useful for migrations, but they can match more URLs than intended. Anchor patterns with ^ and $ where appropriate, group only the text you need to carry forward, and document the expected input and output.

# Apache: move numeric article URLs
RewriteRule ^articles/([0-9]+)/?$ https://www.example.co.uk/guides/$1/ [R=301,L]
# Nginx: the same basic capture pattern
rewrite ^/articles/([0-9]+)/?$ /guides/$1/ permanent;

Apache and Nginx do not use identical escaping, capture or rule-processing conventions. Treat a copied rule as a starting point, not as a drop-in guarantee. Include representative edge cases in your test list.

4. Enforce HTTPS

HTTP-to-HTTPS enforcement normally belongs in a separate HTTP server or virtual host that redirects every request to the secure scheme. In Apache, a common .htaccess pattern is:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [R=301,L]

In Nginx, use a dedicated port-80 server block:

server {
    listen 80;
    server_name example.co.uk www.example.co.uk;
    return 301 https://www.example.co.uk$request_uri;
}

The HTTPS server block should then serve the site or apply the remaining canonicalisation rules. If TLS is terminated at a load balancer or CDN, check how the origin receives the original scheme before adding an HTTPS condition; otherwise, a redirect loop may result. Our HTTP, HTTPS and www loop troubleshooting guide covers this diagnostic process.

5. Choose a www or non-www canonical host

Use one preferred hostname consistently in redirects, internal links, XML sitemaps and canonical tags. An Nginx example that redirects the bare domain to the www host is:

server {
    listen 443 ssl;
    server_name example.co.uk;
    return 301 https://www.example.co.uk$request_uri;
}

In Apache, the equivalent condition can be written as:

RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [R=301,L]

Only use these examples after adapting the hostnames, certificates and proxy arrangement. A certificate must cover every HTTPS hostname that users reach before the redirect can be completed cleanly.

6. Preserve, replace or remove query strings

Query-string behaviour deserves explicit testing. A simple redirect commonly carries the original query string to the destination. That may be useful for tracking parameters, but it may also pass obsolete parameters or create duplicate URL variants.

In Apache 2.4, QSD discards the incoming query string:

RewriteRule ^old-page/?$ https://www.example.co.uk/new-page/ [R=301,L,QSD]

To replace it, include the new query string in the substitution and use QSD only when you specifically want to discard the old one. In Nginx, a return using $request_uri preserves the original path and query string. A rewrite replacement ending in a question mark has historically been used to remove arguments, but behaviour and syntax can vary by version and configuration, so confirm the running Nginx version and test the response rather than relying on memory.

Keep campaign parameters intentional. For a deeper treatment, read UTM tracking without losing SEO.

What to double-check before deployment

  • Configuration location: Confirm whether the rule belongs in .htaccess, an Apache virtual host, an Nginx server block, a CDN, a control panel or an application layer.
  • Module and permission support: Apache rewrite rules require the relevant module and permitted overrides. Some managed hosts restrict directives in .htaccess.
  • Rule order: Put specific exceptions before broad patterns. A catch-all rule can prevent later rules from running. Review the guidance on redirect rule order and precedence.
  • Status and destination: Check that the response is the intended 301 or 302 and that the final destination returns a successful response rather than another redirect.
  • Chains: Redirect the original URL directly to its final destination where possible. Use a redirect checker to inspect status codes, hops and headers.
  • Loops: Test HTTP, HTTPS, www and non-www combinations, particularly when a proxy or CDN sits in front of the origin.
  • Escaping: Check spaces, encoded characters, dots, question marks, ampersands and backslashes. Regular expressions and URL syntax do not treat these characters identically.
  • Cache effects: A cached permanent redirect can make a corrected rule appear not to work. Test with a suitable client, inspect response headers and consider the caching layer before changing working configuration.

Before a large migration, prepare a redirect map containing the old URL, intended destination, status code, rule owner and test result. The redirect mapping and pre-launch checklist can be used as a starting point.

Common mistakes

  1. Mixing Apache and Nginx syntax: A RewriteRule pasted into an Nginx configuration will not work, and an Nginx return directive is not an Apache rule.
  2. Using a relative destination unintentionally: For cross-host redirects, use a complete HTTPS destination so the result is unambiguous.
  3. Creating a chain during a migration: If /old redirects to /intermediate, which redirects to /new, update the first rule to point directly to /new when practical.
  4. Putting a broad rule first: A root, folder or catch-all rule can capture URLs that require a more specific destination.
  5. Ignoring query strings: Decide whether tracking parameters should be retained, transformed or removed. Do not let accidental parameters determine SEO or analytics behaviour.
  6. Testing only in a browser: Browsers cache redirects and hide useful details. Use command-line requests such as curl -I and curl -IL, then test from more than one network or cache context where relevant.
  7. Reloading without validating syntax: Run nginx -t for Nginx or apachectl configtest for Apache before reloading. Use the equivalent command supplied by the host if those commands are unavailable.

When to revisit

Review redirect rules before a domain change, HTTPS rollout, rebrand, URL restructure, server migration, CMS change or seasonal campaign that changes landing pages. Recheck them when a CDN, reverse proxy, hosting panel or deployment workflow changes, because the layer that handles redirects may have moved.

After deployment, sample important old URLs and confirm the complete chain, final status, hostname, scheme, path and query string. Repeat the check after cache expiry or configuration propagation where applicable. For larger changes, compare crawl results, server logs and analytics before and after the release; a drop in requests to expected destinations can reveal a missing rule even when a few manual tests pass.

Keep a dated copy of the active configuration and redirect map. When a rule is retired, record why and verify that no internal links, campaign URLs, QR codes or external references still depend on it. This makes the checklist useful not only for the next migration, but also for routine maintenance and troubleshooting.

Practical final check: validate the configuration, request representative URLs with and without query strings, test every HTTP and hostname variant, inspect the final response with a redirect checker, then monitor logs after release. If the result is not a single, intentional redirect to the correct final URL, pause and fix the rule before expanding it to more paths.

Related Topics

#Apache#Nginx#htaccess#web servers#developer guide
R

Redirect Portal Editorial Team

Technical SEO Editors

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.