Friendly URL with Nginx

2

I would like your help because I did not get the expected result.

I'm trying to create a friendly URL like below:

Current URL:

meusite.com.br/artista.php?id_artista=1

New URL:

meusite.com.br/nome-do-artista

Being that later I will have:

meusite.com.br/nome-do-artista/discografia
meusite.com.br/nome-do-artista/musicas
meusite.com.br/nome-do-artista/informacoes

The most I could do was leave the URL like this:

meusite.com.br/artista/nome-do-artista

Nginx

rewrite ^/artista/([a-z]+) /artista.php?artista=$1 last;
    
asked by anonymous 24.08.2015 / 14:16

1 answer

1

Resolution:

Your rule:

rewrite ^/artista/([a-z]+) /artista.php?artista=$1 last;

You are redirecting the URL:

meusite.com.br/artista/nomedoartista

To remove the /artista/ snippet from the URL, simply remove it from the

rewrite ^([a-z]+) /artista.php?artista=$1 last;

Another observation in your rule, more specifically in the regular expression is that [a-z] only searches for letters from a to z . (I'm not sure if it's case sensitive )

In the documentation examples use (.*) which means any character, but I'd advise using [\w\-_]+ or [a-zA-Z0-9\-_]+ that takes alphanumeric characters (uppercase and lowercase), numbers, dash (-) and underscore (_):

rewrite ^([\w\-_]+)/?$ /artista.php?artista=$1 last;

Note: The /? excerpt means that there may or may not be a / bar at the end.

The rule to get the URLs with the other parameters would be:

rewrite ^([\w\-_]+)/([\w\-_]+)/?$ /artista.php?artista=$1&info=$2 last;

//               $artista        $info
//meusite.com.br/nome-do-artista/discografia
//meusite.com.br/nome-do-artista/musicas
//meusite.com.br/nome-do-artista/informacoes

Note: I've never worked with Ngnix, the examples posted here were based only on regular expressions (which I believe are the same for Apache mod_rewrite and Ngnix), have not validated, nor tested module syntax rewrite of Ngnix.

Related

Friendly URL using HTACCESS

Problem with form (GET method) and Friendly URL

    
24.08.2015 / 19:16