rewriting $arg into request.

Francis Daly francis at daoine.org
Fri Jan 31 11:28:43 UTC 2020


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.)

http://nginx.org/r/map
http://nginx.org/r/$request_uri

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list