Welcome! Log In Create A New Profile

Advanced

nginx rewrite rule problem

Posted by Xhahust 
nginx rewrite rule problem
May 17, 2011 08:33AM
Hello guys.

i have a site that looks so :

http://example.com/catalog.php?category=1

but i want that the site looks so :

http://example.com/catalog/1

and :

http://example.com/item.php?i=1

->

http://example.com/item/1

I hope you can help me.


my settings :


location / {
root /var/www;
index index.php index.html index.htm;
#auth_basic "Restricted";
#auth_basic_user_file /var/www/.htpasswd;
if (!-e $request_filename) {
rewrite ^/(.*)$ catalog.php?category=$1 last;
}
}



Edited 3 time(s). Last edit at 05/17/2011 08:37AM by Xhahust.
Re: nginx rewrite rule problem
July 17, 2011 01:44PM
Try the below:

location ~ ^/catalog/(?P<catnum>\d+)$ {
include fastcgi_params;
fastcgi_param QUERY_STRING category=$catnum;
fastcgi_param SCRIPT_FILENAME $document_root/catalog.php;
fastcgi_pass phpfarm; # replace phpfarm with your FastCGI server
}

location ~ ^/item/(?P<itemnum>\d+)$ {
include fastcgi_params;
fastcgi_param QUERY_STRING category=$catnum;
fastcgi_param SCRIPT_FILENAME $document_root/item.php;
fastcgi_pass phpfarm; # replace phpfarm with your FastCGI server
}

if ($request_uri ~ ^\/catalog\.php\?category=(?P<catnum>\d+)$) {
rewrite ^ $scheme://$http_host/catalog/$catnum? permanent;
}
if ($request_uri ~ ^\/item\.php\?i=(?P<itemnum>\d+)$) {
rewrite ^ $scheme://$http_host/item/$itemnum? permanent;
}

# the below rule is only required if you have further
# rewrite rules that direct anything to the specified
# url
#
# if ($request_uri ~ ^/(catalog|item)/\d+$) {
# break;
# }


Please note that it is not the best implementation. Best would be to change your PHP scripts that they support and handle URL's like "/catalog/12" or "/item/222" directly, instead of "/catalog.php?category=12" and "/item.php?i=12".

If you like, I could change the scripts for you, for a little fee. :)

Hope it helps,

Andrejs
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 100
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready