Welcome! Log In Create A New Profile


Clean URL with slash arguments?

Posted by oo 
Clean URL with slash arguments?
February 07, 2013 08:16AM

Apache (at least apache2) seems to do this automatically on default, but I have tried to figure out how to mimic it under Nginx for serveral days now. Maybe this is the place where someone has the answer how to do this:

1) I have for example http://localhost/user/edit.php
2) I have for example http://localhost/admin/organization/edit.php

Both take several get-parameters and if they get them in "slash format", it's ok (I'm using my own MVC framework).

I would like to have a) clean urls (no .php extension) and slash arguments at the same time, so that:

1) http://localhost/user/edit/id/1 would work
2) http://localhost/admin/organization/edit/id/1/something/else works

The problem for me is, that with "try_files" or "rewrite" and/or regex in "fastcgi_split_path_info" it's hard to detect, which part is the actual script and which are the get-parameters.

A beer at our local bar for the friendly person who helps me out here (in Helsinki Finland)! ;)


Edited 1 time(s). Last edit at 02/07/2013 08:18AM by oo.
Re: Clean URL with slash arguments?
February 14, 2013 02:30AM
Hey again,

I'm replying to my own post, but I got the answer (except one minor thing). After realizing it was Apache's Multiviews enabling the clean urls automatically, I started to "google" with "nginx multiviews" ending up with this configuration:

server {
index index.php index.html;
server_name subdomain.domain.com;

access_log /var/log/nginx/subdomain.domain.access_log;
error_log /var/log/nginx/subdomain.domain.error_log warn;

root /var/www/html/subdomain.domain/view;

location ~ ^(.*)/ {
try_files $uri $uri.php $uri/ $1?$args $1.php?$args;
fastcgi_index index.php;
include fastcgi_params;

It works perfectly, except "directory index". If I just enter "directory name" as the url, it does not load "index.php" (even if there is fastcgi_index index.php; set there). I would appreciate, if someone guided me a bit here, thanks!

here's the .htaccess for Apache setting Multiviews propelly (it doesn't work just by setting Options +Multiviews):

Options +MultiViews
AcceptPathInfo ON
AddType application/x-httpd-php .php

(I'm not sure if AcceptPathInfo ON is required or not (helps with the ending /), but setting the mimetype is required).
Re: Clean URL with slash arguments?
March 03, 2013 03:17PM

I'll reply once more to my own thread. I found (some time ago) a solution for the "directory index" problem I had - and I thought I'll post it here if someone is struggling with the same problem.

Addin $uri/index.php to the "try_files" statement does the trick. Like this:
try_files $uri $uri.php $uri/index.php $uri/ $1?$args $1.php?$args;

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 87
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