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

I conisder moving to Nginx but I want you to ask if is possible to rewrite urls into that schema, and if you could help me a bit:

  • A url like example.com/username into profile.php?u=username. But then, will I have problems in accessing other pages like e.g. home.php

  • A url like example.com/questions/102039/that-question into questions.php?quid=102039

Thank you very much!

share|improve this question

1 Answer 1

If "username" is only letters and numbers, no .s or /s, then you can probably create a rule something like

rewrite ^/([A-Za-z0-9]+)$ /profile.php?u=$1 last;

The first half of that command is the request from the client. The second half is what we want to convert that request to. last means to stop trying to rewrite this request.

If you're not familiar with regular expressions, [A-Za-z0-9] essentially means one character between A and Z or a and z or 0 and 9. The + afterwards expands that to mean "a string of at least one in a row". By wrapping it in () we are able to save that string, and refer to it as $1 in the second half (if we had more than one sets of parentheses then the second would be $2 and so on). Finally, the ^ means the beginning of the request, and the $ means the endig. So, a request for /home.php would not be rewritten because it does not start with / followed by a string of letters and numbers that reach to the end of the URL because of the . in the middle.

I've seen people use

if (!-e $request_filename) {
  rewrite ...;
}

which solves the problem of "what if there really was a file named that" without relying on specific username patterns.

The second one could be

rewrite ^/questions/([0-9]+)/ /questions.php?quid=$1 last;

Note that this one didn't use the $, since we don't care about how the URL ends, all we need is that it starts with /questions/ and has a number, followed by a slash..

You can read more about nginix's rewriting capability here.

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.