Friendly URL with Nginx - problem with files

3

I will try to be as explicit as possible.

Friends, I am using Nginx as a local server and I have been modifying the urls in a friendly way, but I have a problem, could you help me with this solution? What am I doing wrong?

I inform that all my projects are in the www (localhost) folder

I have managed to make a url remain: http//localhost/miproyecto/producto/1000

Using the following code in nginx.conf:

location /miproyecto/producto/ { 
    index index.php; 
    try_files $uri $uri/ /miproyecto/producto/index.php?$args; 
}

I also deleted all the extensions of the files.

http//localhost/miproyecto/busqueda.php

A:

http//localhost/miproyecto/busqueda

With the following code:

location / {
    index  index.php index.html;
    try_files $uri $uri.html $uri/ @extensionless-php;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

Here comes my problem and I do not know how to fix it (Clarify with Apache works great for me)

Instead of this url:

http//localhost/miproyecto/negocio.php?empresa=nombreEmpresa

I want it to look like this:

http//localhost/miproyecto/nombreEmpresa

I achieved something using this code:

location /miproyecto/ {
    if (!-f $request_filename) {
        rewrite ^/miproyecto/(.*)+$ /miproyecto/negocio.php?$1;
    }
    index index.php 
}

But my problem here is that now if I enter another url for example:

http//localhost/miproyecto/registro

It does not open the corresponding file, ie it does not open at registro.php but thinks that I'm sending the word 'registration' as a variable or company name. Nor does it take me the index.php that is, if I put

http//localhost/miproyecto/

It does not redirect me to the index.php file.

And if I modify the code to this:

location /miproyecto/negocio {
    index index.php;
    rewrite ^/miproyecto/(.*)+$ /miproyecto/negocio.php?$1;
}

If you recognize me the urls that I mentioned before but you can not place it in the form localhost/miproyecto/nombreEmpresa

It does not allow me to do both.

    
asked by Roberto Esteban 01.07.2016 в 22:10
source

1 answer

1

If instead of using directories for your location entries you would use regular expressions, it would actually be possible to do what you propose, since nginx interprets the expressions giving priority to the most specific ones over the less specific ones, then

location ~ ^/miproyecto/negocio {
  rewrite ^/miproyecto/(.*)+$ /miproyecto/negocio.php?$1;
}

would have priority over

location ~ ^/miproyecto/ {
    if (!-f $request_filename) {
        rewrite ^/miproyecto/(.*)+$ /miproyecto/negocio.php?$1;
    }
}

But just as you have it, except to play with the order in which you put the rules, I do not think you can do more.

If you do not dare to use regular expressions for location entries, the only thing I can think of is to suggest that you use another "namespace" for CompanyName:

location /miproyecto/empresa {
    if (!-f $request_filename) {
        rewrite ^/miproyecto/(.*)+$ /miproyecto/negocio.php?$1;
    }
    index index.php 
}
    
answered by 23.02.2017 в 16:51