Search code examples
nginxurlparametersspecial-charactersslash

Pass varible with slash into nginx


nginx rewiting resolve slash in variable. How to do with nginx to pass a content parameter a slash (/) ? ex: /1524/Ri/Hr/p2 (Ri/Hr is the parameter)

So THX


Solution

  • Depending on your PHP backend, if it isn't something that relies no the REQUEST_URI FastCGI variable only (making any kind of rewrite rules senseless, it is the REQUEST_URI that should be tweaked instead in that case), you can try to use the non-normalized $request_uri variable instead:

    if ($request_uri ~ /(?<nid>\d+)/(?<label>[^/]+)/p(?<page>\d+)/?(?:\?|$)) {
        rewrite ^ /index.php?nid=$nid&label=$label&page=$page last;
    }
    

    The named captures usage instead of numbered ones is essential here since all the numbered captures and corresponding variables will be overwritten by the rewrite directive itself during the regex matching upon the ^ pattern.