How can I do nginx rewrite?

How can we append an extra 'path' after base domain in nginx rewrite such as: http://www.thissite.com to http://www.thissite.com/main (so does http://www.thissite.com/hallo to http://www.thissite.com/main/hallo and so on)?

  • Answer:

    I am not exactly sure what you are trying to accomplish, however if all you want to do is append a url such as http://www.this-domain.comm[/code] in such a way that it is becomes www.this-domain.com/ You could do this with the following php snippet: <?php $dnm1="www.this-domain.com"; if (preg_match('/\b([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\b/i', $dnm1)) { # Successful match $dnm=$dnm1."/"; echo $dnm; } else { # Match attempt failed end; } ?> The above regex is explained: Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) «\b» Match the regex below and capture its match into backreference number 1 «([a-z0-9]+(-[a-z0-9]+)*\.)+»    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»    Match a single character present in the list below «[a-z0-9]+»       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»       A character in the range between “a” and “z” (case insensitive) «a-z»       A character in the range between “0” and “9” «0-9»    Match the regex below and capture its match into backreference number 2 «(-[a-z0-9]+)*»       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»       Match the character “-” literally «-»       Match a single character present in the list below «[a-z0-9]+»          Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»          A character in the range between “a” and “z” (case insensitive) «a-z»          A character in the range between “0” and “9” «0-9»    Match the character “.” literally «\.» Match a single character in the range between “a” and “z” (case insensitive) «[a-z]{2,}»    Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}» Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) «\b»

Steve Kinzey at Quora Visit the source

Was this solution helpful to you?

Other answers

location / { rewrite ^ http://http://www.thissite.com//main permanent; } location /hallo { rewrite ^ http://http://www.thissite.com//main/hallo permanent; }

Rick WR

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.