Welcome! Log In Create A New Profile

Advanced

nginx rewrite file.php to page.php?page=file

Posted by Marwelln 
nginx rewrite file.php to page.php?page=file
October 19, 2011 05:48AM
I am really new to nginx and wanted to setup a rewrite but it allways fails for me. What I want is to rewrite file.php to page.php?page=file, but I only want this to occour if file.php dont exist. Can anyone help me with this?

I've tried a few rewrites myself but nothing works...
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 07:20AM
location ~ file\.php {
try_files $uri page.php?page=file;
}

Andrejs



Edited 3 time(s). Last edit at 10/19/2011 07:22AM by locojohn.
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 07:30AM
"file" is dynamic, not static as I assume your code is.
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 07:32AM
Sorry, I was too quick. Here's the fix:


location ~ (.*/)file\.php {
try_files $uri $1page.php?page=file;
include fastcgi_params;
fastcgi_pass ...; # path to fastcgi interface (unix socket or IP)
}



Edited 1 time(s). Last edit at 10/19/2011 07:35AM by locojohn.
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 07:55AM
Thank you my friend. I got it working with this:

location ~ (.*/)([a-zA-Z0-9\_\-\.]+).php {
try_files $uri $1page.php?page=$2;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 08:01AM
Glad it worked. Just noticed a small thing about the "\.php" vs ".php" as "." means any character in PCRE:

location ~ (.*/)([a-zA-Z0-9\_\-\.]+)\.php {

Also, suggest to shorten it a bit for nicety:

location ~ (.*/)([\w\-\.]+)\.php {

Andrejs
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 08:16AM
Just noticed the query string isn't working. Is there a fix to this?

file.php?foo=bar should be page.php?page=file&foo=bar

But in my case, foo (the query string) isnt registered.
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 08:21AM
try_files $uri $1page.php?page=$2&$args;

Andrejs
Re: nginx rewrite file.php to page.php?page=file
October 19, 2011 08:23AM
Splendid. It worked.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 92
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready