0

I'm venturing now into nginx rewrite rules so it's a fairly new topic for me.

I have in the root folder a series of pages which i use rewrite rules as follows:

server {
listen 80;
## listen [::]:80 default_server ipv6only=on;
listen 443 ssl;

root /nginx/gtt/;
index index.html index.htm index.php gttindex.php;

# Make site accessible from http://localhost/
server_name gtt.deb;

ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

client_max_body_size 10M;


# When access gps page just show gps
if ( $request_uri = "/gps" ) {
    rewrite ^ /gps.lf.ws.vh.sprite.php break;
}
# Administration
if ( $request_uri = "/setup" ) {
    rewrite ^ /admin.php break;
}
# For clientadmin show admin
if ( $request_uri = "/error" ) {
    rewrite ^ /gttindex.php?WARNING=wrongcredentials break;
} 

rewrite ^/cargo(.*)$/(.*)$ /clientadmin/edit_task_details_form_multi$1.php?taskid=$2 break;
rewrite ^/cargoadmin    /clientadmin/admin.php last;

location / {
    index index.html index.htm index.php gttindex.php;
    try_files $uri $uri/ =404;
}


}

will redirect to admin.php

The problem comes when I have to rewrite in a subfolder that contains 4 php scripts:

admin.php
edit_task_details_form.php
edit_task_details_form_multi2.php
edit_task_details_form_multi3.php

It doesn't work and /cargo2/var or /cargo3/var (depending on which i select) are being appended to the url.

What I'd like to achieve is the following:

http://gtt.deb/clientadmin/admin.php -> http://gtt.deb/cargoadmin

http://gtt.deb/clientadmin/edit_task_details_form_multi.php?task=1 -> http://gtt.deb/cargo/1

http://gtt.deb/clientadmin/edit_task_details_form_multi2.php?task=1 -> http://gtt.deb/cargo2/1

http://gtt.deb/clientadmin/edit_task_details_form_multi3.php?task=1 -> http://gtt.deb/cargo3/1

6
  • Please show more concrete examples, what URL are you retrieving and what file is run when you try to use the rule you show here. May 13, 2015 at 16:15
  • Added server section configuration to question. I'm testing on my home laptop before deploying May 13, 2015 at 16:19
  • It is still unclear, what is the exact URL you are trying to load, what is the exact file that should be sent, and what is the exact file that is sent with the current rules. Add a single example with all these three facts, then it is possible to find out the problem. May 13, 2015 at 16:23
  • The base usr of the website is gtt.deb as said its on a local server and the 4 scripts are under gtt.deb/clientadmin May 13, 2015 at 16:24
  • I'll try one last time.. What I need is an example like this: 1. Request URL: http://somesite/cargo2/something 2. Wanted target file: /nginx/gtt/somefile.php?someparam=something 3. Target file that current setup provides: /nginx/gtt/somefile.php?someparam=1&somethingelse . Show this concrete example, then it is possible to understand WHAT is the exact problem you have. May 13, 2015 at 16:28

1 Answer 1

1

You are using the rewrite directive wrong. You don't use if with the rewrite rules.

You should write your current rules like this:

rewrite ^/gps$ /gps.lf.ws.vh.sprite.php break;
rewrite ^/setup$ /admin.php break;
rewrite ^/error$ /gttindex.php?WARNING=wrongcredentials break;

And most likely you should use last instead of break in the rules.

Regarding the actual issue, try a rewrite like this:

rewrite ^/cargoadmin$ /clientadmin/admin.php;
rewrite ^/cargo([0-9]?)/([0-9]+)$ /clientadmin/edit_task_details_form_multi$1.php?task=$2;

The ([0-9]?) matches all single-digit numbers or an empty string, and captures the number to $1. The ([0-9]+) matches multiple-digit numbers, and captures it into $2 variable.

The $ means that the string must end there, there cannot be any extra characters after that.

On another note, your URL system looks quite complex and it might well cause lots of problems later on. I would pay more attention to develop a simpler and more well defined system to the URL rewriting.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .