2 Replies - 279 Views - Last Post: 28 February 2019 - 12:03 PM

#1 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 106
  • Joined: 05-June 09

How NGINX rewrite works?

Posted 28 February 2019 - 12:07 AM

Ok so I want my site to not show the extension of my files for example: example.com/login instead of example.com/login.php or example.com/p/4 instead of example.com/pages.php?id=4

I sort of get how I'd do the latter (which I'd think would be harder), something like this:

example.com/linux/1

rewrite ^/linux/(.*)$ /linux.php?distro=$1 last;


But thats not exactly what I'd need to do to just remove the .php from login.php, I tried this and NGINX wouldnt restart because of bad syntax:

location /login { 
try_files $document_root/login.php;
}


Could someone explain how I'd achieve this and point me to an easy to understand guide to how regex works? Thanks

This post has been edited by trickstar34: 28 February 2019 - 12:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How NGINX rewrite works?

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6794
  • View blog
  • Posts: 31,349
  • Joined: 10-May 07

Re: How NGINX rewrite works?

Posted 28 February 2019 - 10:34 AM

Why are you using last; in the rewrite? I always use permanent to enforce the 301. I've not seen last; nor do I know what it does.

For my relative root, I have the following :

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



This says to try the provided file with the .html extension, & if it does not exist throw index.html as the 404 page.
Was This Post Helpful? 0
  • +
  • -

#3 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 106
  • Joined: 05-June 09

Re: How NGINX rewrite works?

Posted 28 February 2019 - 12:03 PM

View Postno2pencil, on 28 February 2019 - 10:34 AM, said:

Why are you using last; in the rewrite? I always use permanent to enforce the 301. I've not seen last; nor do I know what it does.

For my relative root, I have the following :

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



This says to try the provided file with the .html extension, & if it does not exist throw index.html as the 404 page.


It was from this tutorial I'm trying to follow.

https://www.thegeeks...write-examples/

I know how to make a standard index and set up PHP and all that, I'm trying to do "pretty URLs". Like how this site every page says index.php?app=forum.. I want it to be something like dic.net/forums/topic/[topicid] instead. Or like I said in my original post I'm also trying to make standard pages without URL data like login.php come up from just typing [site name]/login vs using [site name]/login.php
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1