Welcome! Log In Create A New Profile

Advanced

Rewrite rules

Posted by danchodanchev 
Rewrite rules
April 06, 2011 04:55PM
Hello

I recently decided to move from Apache to Nginx but having some problems getting my rewrite rules correct.

In apache the where:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/([^/]+)\//?$ %{DOCUMENT_ROOT}/data.php?type=$1&data=$2 [L]

And my nginx config looks like:

server {

listen 80;

server_name localhost;

location / {

root html;

index index.html index.htm;

if (!-d $document_root$request_filename) {

if (!-f $document_root$request_filename) {

rewrite ^/([^/]+)/([^/]+)\//?$ $document_root/data.php?type=$1&data=$2 last;

}

}

}

}


I would creatly apprechiate if someone could help me out with this, it does not seem to have any effect at all when visting for example http://localhost/thisone/doesnotexist/

And also is there a way to debug the rules?
Re: Rewrite rules
April 14, 2011 05:03AM
I think you cannot nest the if directive.
following would work ( by syntax, didnt check the regex and variables )

if (!-d $document_root$request_filename) {
rewrite ^/([^/]+)/([^/]+)\//?$ $document_root/data.php?type=$1&data=$2 last;
}

if (!-f $document_root$request_filename) {
rewrite ^/([^/]+)/([^/]+)\//?$ $document_root/data.php?type=$1&data=$2 last;
}

I'd say you wont need the first one, ( is_directory ) when you dont use directory listing, a request on /x/y/ would request the index.htm defined in the index directive and !-f would evaluate true ( needs checking tho )
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 155
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready