1

I tried to convert this htaccess rules to nginx:

apache

# Necessary to prevent problems when using a controller named "index" and having a root index.php
# more here: http://httpd.apache.org/docs/2.2/content-negotiation.html
Options -MultiViews

# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On

# Disallows others to look directly into /public/ folder
Options -Indexes

# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then leave it commented out
RewriteBase /php-mvc/

# General rewrite rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

nginx

server {
    listen  80;
    listen  [::]:80 default_server ipv6only=on;

    root /home/goggelz/www;
    server_name localhost;
    rewrite_log on;

    location / {
        index index.php;
    }

    location /php-mvc {
        if (!-d $request_filename){
            set $rule_0 1$rule_0;
        }
        if (!-f $request_filename){
            set $rule_0 2$rule_0;
        }
        if (!-e $request_filename){
            set $rule_0 3$rule_0;
        }
        if ($rule_0 = "321"){
            rewrite^/(.+)$ /php-mvc/index.php?url=$1 last; 
        }

    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

    }
}

The thing is, I want to use the rewrites only in the /home/goggelz/www/php-mvc folder, and another issue: When I try to open localhost, then the index.php file is being downloaded but when I try to connect with 127.0.0.1, the index.php executes. How can i fix those problems?

1 Answer 1

1

This is a version that uses nginx try_files directive.

server {
    listen  80;
    listen  [::]:80 default_server ipv6only=on;

    root /home/goggelz/www;
    server_name localhost;
    rewrite_log on;

    index index.php;

    location /php-mvc {
        try_files $uri $uri/ @mvcrewrite;
    }

    location @mvcrewrite {
        rewrite ^ /php-mvc/index.php?url=$request_uri last; 
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Here, the try_files is used to first check if the requested URI exists as a file or directory. If it exists, it is sent. If it does not exist, request is forwarded to the @mvcrewrite block, where the request is rewritten to pass the URI to the index.php

This might also fix the issue with opening localhost.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .