I'm trying to filter out multiple directories from URL with Nginx but to no avail. All instructions I saw on web are only relevant when only one directory is to be removed but nobody shows instructions for multiple.
This is the URL:
https://www.domain.com/pages/l/something.html
What I want is:
https://www.domain.com/something.html
Note the removed /pages/l/ segment.
Also, I need URL rewritten and 301 (permanent) redirected so no SEO is harmed.
How do I achieve this?
This is a rewrite rule to make nginx send a
301
redirect to the browser when the user accesses any file / path under/pages/l/
:Here we capture the path after
/pages/l
with regex, and then use it as the redirect destination argument.If you want to make matches for example any one letter directory under
/pages/
and any file under that, you can use this:If the files are static HTML files that cannot be moved from their current directory locations to places that correspond to new URLs, you can tweak your
try_files
directive like this:Here we tell nginx to look up for the actual files in path specified by the URI, or if not found, then with the
/pages/l
prefix.However, I really recommend that you physically move the files, as this modification adds complexity to the system and makes it more difficult to maintain. After all, the requests to the files are
301
redirected to the new location, so users see no difference.