User-friendly URL with Nginx

Asked

Viewed 2,856 times

2

I would like some help from you because I did not arrive at the expected result.

I am trying to create a friendly URL as below:

Current URL:

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

NEW URL:

meusite.com.br/nome-do-artista

And then I’ll have:

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

All 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;
  • This rule is rewriting this URL meusite.com.br/artista/nome-do-artista, Isn’t that what you want? Nginx is the equivalent of Apache?

  • @Kaduamaral exactly, Nginx is similar to Apache. The way this rule stands, it works perfectly for the case meusite.com.br/artista/joao, but I would like to change to meusite.com.br/joao

  • So just take the part /artista/ of the @Ricardo rule. ;)

  • You must specify the CSS path from the base of the URL using the slider /, example /css/meucss.css or using the absolute URL meusite.com.br/css/meucss.css. For more details please read the comments of this reply.

  • @Kaduamaral, thanks! I will test here

1 answer

2


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 snippet /artista/ of the URL, just delete it from the rule

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

Another remark in his rule, more specifically in the regular expression, is that [a-z] search only letters of a à z. (I’m not so sure it is either case sensitive)

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

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

Obs.: The excerpt /? means there may or may not be a bar / in 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 have 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), not Validei, nor tested the syntax of the module rewrite ngnix.


Related

User-friendly URL using HTACCESS

Problem with form (GET method) and URL Friendly

  • even putting the absolute CSS URL it got all messed up. probably because the path is www.meusite.com.br/images/... does he understand that the /images/ be the name of some singer?

  • It is possible, in htaccess there are conditions that checks if the URL accessed is a file. I did a web search and found this condition of Ngnix if (!-f $request_filename), put your rules within that condition and check again. Another test is to access the address of the CSS files in the browser and check what appears @Ricardo

  • It worked perfectly with the rules within this @Kadu IF. Thanks!

  • The only thing that didn’t work is when I wear something after the singer’s name, like www.meusite.com.br/joao/discografia. I did so: rewrite ^([a-z]+)/discografia /discografia.php?artista=$1 last;

  • @Ricardo use the flag break instead of last and see if it works... Fountain -> Wiki

  • Thus rewrite ^([a-z]+)/discografia /discografia.php?artista=$1 break;? If it is, it opens the file download window..

  • @Ricardo tested without flags?

  • No, I’ll test it tomorrow and tell you!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.