nginx url rewriting. i just don't get it

17,760

Solution 1

I believe that this website may help you greatly:

http://regex101.com/r/uP4nT1

Solution 2

You need

location /yourlocations {
   if ($args ~* "p=[a-z]*&view=[a-z]*") {
        rewrite ^ http://yourwebsite.com/$arg_p/$arg_view? last;
        }

}

Also if you want to capture the "view" you should make the program that view is an argument like: yourwebsite.com/?p=test&second=test2&third=test3 so you can have the following thing:

location /yourlocations {
       if ($args ~* "p=[a-z]*&second=[a-z]*&third=[a-z]*") {
            rewrite ^ http://yourwebsite.com/$arg_p/$arg_second/$arg_third? last;
       }

}
Share:
17,760

Related videos on Youtube

r3wt
Author by

r3wt

Updated on September 18, 2022

Comments

  • r3wt
    r3wt over 1 year

    Ok, an example url is

    http://www.mysite.com/?p=account&view=settings

    the p uri is a constant. all urls have it.

    the view is one of many optional uri.

    i've tried to understand url rewriting and regex, but i'm just not getting it. I need to be back to writing code, but unfortunately i just keep coming back to nginx.conf for more punishment.

    my goal, is to rewrite the urls as so:

    http://www.mysite.com/account/view/settings

    I would show you the things i tried but its just copy and pasting alot of previous examples, proving how pathetic I am at grasping the concepts of regex and url rewriting.

    If someone could take a few minutes to explain the regex part, or link me to a good tutorial on understanding it i would appreciate.

    No, i don't expect you to do the work for me, but i humbly ask for a little help grasping the concepts of rewriting and the variables to use in nginx conf. I can see that one is called $uri, but have no idea what any of it means. I also need to add a regex to deny direct access of php scripts in the /socket and /private, but allow access via ajax. i'm sure that i can probably apply whatever knowledge learned about regex to that task.

    the location blocks of my nginx conf

    location ~ \.(hh|php)$ {
        fastcgi_keep_conn on;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~* .(png|gif|ico|css|js)$ {
        expires 365d;
    }   
    location ~ .(aspx|jsp|cgi)$ {
        return 410;
    }
    location /socket {
        return 405;
    }
    location /private {
        return 405;
    }   
    location / {
        # include /etc/nginx/naxsi.rules;
        index index.php;
        try_files    $uri $uri/ /index.php?q=$uri?;
        limit_req zone=one burst=5;
    }
    
    location /Denied {
        return 418;
    }
    error_page 500 /error.php?type=500;
    error_page 501 /error.php?type=501;
    error_page 502 /error.php?type=502;
    error_page 503 /error.php?type=503;
    error_page 400 /error.php?type=400;
    error_page 401 /error.php?type=401;
    error_page 403 /error.php?type=403;
    error_page 404 /error.php?type=404;
    error_page 405 /error.php?type=405;
    error_page 406 /error.php?type=406;
    error_page 413 /error.php?type=413;
    error_page 414 /error.php?type=414;
    error_page 418 /error.php?type=418;
    
  • Vasili Syrakis
    Vasili Syrakis almost 10 years
    That's ok. Were you able to apply it to your rewrite rules?
  • r3wt
    r3wt almost 10 years
    No, i haven't tried yet. Thanks to the syntax highlighting and the explanation i'm beginning to grasp whats going on in the regex. unfortunately, view is only an argument of account. i also have a few more to rewrite
  • Vasili Syrakis
    Vasili Syrakis almost 10 years
    When you get around to writing some rules, if you need more help I would try the regex tag of stackoverflow, people jump on regex questions very quickly :)
  • r3wt
    r3wt almost 10 years
    based on what you gave me, i've determined that this will work for what i need: ^\?p=([^&]*)&([^&]*)=([^&]*). Now that i know the regex pattern, i still don't know what to put in the nginx.conf.
  • r3wt
    r3wt almost 10 years
    thanks, i tried it but it didn't work
  • Tero Kilkanen
    Tero Kilkanen almost 10 years
    Try to use if ($args ~* "p=[a-z]*&view=[a-z]*"), that is, remove the /? part from the start. $args is the part of the URL after ? sign, so matching /? there is pointless.