nginx url rewriting. i just don't get it
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;
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;
}
}