nginx clean url router/rewrites

Posted by Janko on Server Fault See other posts from Server Fault or by Janko
Published on 2012-09-15T02:52:50Z Indexed on 2012/09/15 3:39 UTC
Read the original article Hit count: 169

Filed under:
|
|
|

im having difficulties with a relativity simple rewrite rules / router in nginx config.

All I want to do is, if requested dir or file 'host/my/request/path[/[index.php]]' does not exist, rewrite to 'host/my/request/path.php'

Current rewrite works for:

host
host/
host/my/request/path

But wont work for:

host/my/request/path/

Here is the rewrite part of the config:

    location = /(.*)/ {
            rewrite ^(.*)$ $1 permanent;
    }

    location / {
             try_files $uri/ $uri $uri.php;
    }

Error log will report:

Access forbidden by rule, request: "GET /my/request/path/ HTTP/1.0"

Hm, is there a better way to solve this or get rid of the trailing slash?

edit, rules more elaborative:

host[/] >  host/index.php
host/index[/] >  host/index.php
host/my/path[/] > if /path/index.php exists: host/my/path/index.php
else host/my/path.php

© Server Fault or respective owner

Related posts about linux

Related posts about php