7

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; 
1
  • If possible, you should have the application handle translating the pretty URLs into whatever needs to be done. Commented May 27, 2014 at 13:35

2 Answers 2

8

I believe that this website may help you greatly:

http://regex101.com/r/uP4nT1

4
  • That's ok. Were you able to apply it to your rewrite rules? Commented May 28, 2014 at 1:22
  • 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 Commented May 28, 2014 at 1:26
  • 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 :) Commented May 28, 2014 at 1:28
  • 1
    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. Commented May 28, 2014 at 1:28
1

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; } } 
2
  • 1
    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. Commented May 28, 2014 at 9:39
  • @TeroKilkanen My bad, wrote it without verifying... Commented May 28, 2014 at 13:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.