Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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

 
discard

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.