Nginx redirect all requests from subdirectory to another subdirectory root

Nginx redirect all requests from subdirectory to another subdirectory root

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Nginx redirect all requests from subdirectory to another subdirectory root

The core issue in your configuration stems from how Nginx evaluates location blocks and processes rewrites, specifically when dealing with PHP locations. You correctly identified that the regex match ~ /store captures more than just /store. It matches any URI starting with /store, including /store/index.php, /store/cart/add, or /store/page?foo=bar.

However, the rewrite rule you provided:

rewrite /trade permanent;

is fundamentally incomplete. In Nginx, a rewrite directive does not automatically include the original URI in the target path unless explicitly specified. When your request hits /store/index.php, Nginx extracts the matched location (/store) and replaces it with /trade. The resulting URI becomes simply /trade, dropping the /index.php part.

This causes two distinct problems:

  1. Missing File Path: The browser requests /trade, which might resolve to index.html or return a 403 depending on your $uri handling, rather than serving the specific PHP script intended for that request.
  2. Parameter Loss: Any query string arguments (e.g., ?id=123) are stripped because you did not append them using the last flag or by preserving them in the replacement string.

The Correct Rewrite Syntax

To redirect the entire path while maintaining the specific file and arguments, you must construct the new URI dynamically. The standard pattern for this is to replace the matched prefix with the target root and append $uri.

Here is the corrected location block:

location ~ /store {
    # Redirect /store/something to /trade/something
    rewrite ^/store/(.*)$ /trade/$1 permanent;
}

Or, using a more concise syntax often preferred for subdirectory redirections:

location ~ ^/store/ {
    rewrite (.*)$ /trade/$1 permanent;
}

This ensures that if the user visits example.com/store/admin, they are redirected to example.com/trade/admin. If they visit example.com/store/cart.php?lang=en, they are redirected to example.com/trade/cart.php?lang=en.

The last Directive Alternative

While rewrite works perfectly fine here, using the last directive is often cleaner for internal redirects or when you want Nginx to re-evaluate the location block after the rewrite. However, for an external permanent redirect (301), rewrite with the permanent flag is standard and efficient.

If you were simply passing traffic through without changing the URL structure significantly, last would be appropriate:

location ~ ^/store/ {
    last;
}

But since your goal is a permanent redirect to a different path (/trade), the rewrite method above remains necessary.

Interaction with PHP Locations

Your concern about the PHP location block overriding the store logic is valid in terms of priority, but the mechanism is slightly different than you described. Nginx processes locations based on the location match priority, not the order they appear in the file (unless using try_files fallbacks).

  1. Specificity First: Nginx matches the most specific location first. Your location ~ /store is a regex match. Your location /trade/ is a prefix match. Your PHP block location ~ \.php$ is also a regex match.
  2. The Rewrite Happens Before Execution: When you issue a rewrite ... permanent, Nginx sends an HTTP 301 response to the client immediately. The client then makes a new request to /trade/.... At this point, the original request lifecycle ends for the server processing the /store block.
  3. No Double Processing: The PHP location block (location ~ \.php$) only processes the new request if the rewritten URI actually contains .php and that specific regex matches. Since you are rewriting to /trade/index.php (or similar), the new request will hit your standard location /trade/ block, which then passes execution to index.php. The original /store logic is never reached again in a single request cycle unless you use an internal redirect without the permanent flag.

Handling Magento Specifics

Since your target is a Magento site, it is crucial that the rewrite preserves the query string. Your current rewrite /trade permanent; strips arguments. If your Magento store relies on session cookies or URL parameters to determine the storefront language, region, or cart state, losing these during the redirect will break the user experience.

The fix using $1 (or appending &$query_string explicitly) ensures data integrity:

rewrite ^/store/(.*)$ /trade/$1 permanent;

This captures everything after /store/ and reconstructs the full path for the Magento application to handle correctly.

Final Configuration Check

Ensure your regex anchor is consistent. Using ^/store/ (with a trailing slash) ensures you don't accidentally match a file named store.txt at the root if such a file existed, though in your case, the directory structure implies /store is the prefix. If you want to match exactly /store without a trailing slash as well as subdirectories, keep the regex broad:

location ~ ^/store/(.*)$ {
    rewrite (.*)$ /trade/$1 permanent;
}

This covers both /store (redirecting to /trade) and /store/admin (redirecting to /trade/admin).

Our network