I am trying to do the following nginx redirect
/p/username
to
/profile/username
seems like it should be simple but I cannot figure out how to isolate the username which can variable.
Unless you actually need the username for something, or you need to redirect only for that exact location and no subdirectories, then it's exceedingly simple - you really don't need to look for the username at all.
rewrite ^/p/(.*)$ /profile/$1 permanent;
But for the sake of argument, if you needed to grab the user name and, say, insert something between the username and the rest of the path, you could do something like this:
rewrite ^/p/([^/]+)(.*)$ /profile/$1/usersite$2 permanent;
The first pair of parentheses will capture everything after /p/
up to (but not including) the following slash; the .*
will capture the rest of the string. They're then added back in at the $1
and $2
- so /p/username/some-page.html
becomes /profile/username/usersite/some-page.html
.