Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to do the following nginx redirect




seems like it should be simple but I cannot figure out how to isolate the username which can variable.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.