rewriting $arg into request.

Steve Wilson lists-nginx at
Fri Jan 31 12:11:54 UTC 2020

Hugo's alias basically creates a /alias/index.html file which contains a 
meta refresh. I managed to find something using an if which does the 
job, however using the map solution presented is much more elegant as it 
would reduce the redirects.

                 if ($args ~ "^p=(\d+)") {
                         set $page $1;
                         set $args "";
                         rewrite ^.*$ /p/$page last;

I knew there'd be a simpler way and I due to the time of night I was 


On 31/01/2020 11:28, Francis Daly wrote:
> On Fri, Jan 31, 2020 at 01:13:30AM +0000, Steve Wilson wrote:
> Hi there,
>> Currently wordpress is using ugly urls for posts, so "/?p=1234" in wordpress
>> might be "/this_nice_title" in hugo.
>> Now hugo allows me to specify aliases too which I'd like to leverage to
>> maintain links, but this is where I seem to be struggling with rewrite/map
>> etc.
>> Am I missing a way to access the arguments?
> Without knowing how hugo works, I would suggest ignoring its "alias"
> feature for this, and just letting nginx invite the client that requests
> "old", to instead request "new".
> Assuming that you have the list of old-and-new urls that you care about,
> and that the old urls are unique case-insensitively, then using a "map"
> reading "$request_uri" (old) and writing, say, "$hugo_url" (new), would
> probably be the simplest.
>      map $request_uri $hugo_url {
>        /?p=1234 /this_nice_title;
>      }
> in http{} (add more lines as wanted), along with something like
>      if ($hugo_url) { return 301 $hugo_url; }
> in the correct server{}, should work, I think. (Untested by me!)
> (Maybe change the "return" line to include "https://this-server$hugo_url",
> if you want that.)
> (If all of your "old" requests have the same content from the first /
> to the ?, then you could choose to isolate the "if" within the matching
> "location = /" block for efficiency; there may be extra config needed
> in that case.)
> Good luck with it,
> 	f

More information about the nginx mailing list