htaccess rewrite rules in Nginx: setting the rewrite path

Posted by ct2k7 on Server Fault See other posts from Server Fault or by ct2k7
Published on 2012-11-18T16:29:04Z Indexed on 2012/11/18 17:03 UTC
Read the original article Hit count: 439

Filed under:
|
|

I have a htaccess file I'm trying to convert into an nignx config file.

Here's my htaccess file.

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule    !\.(jpg|css|js|gif|png)$    public/    [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1

And the rules I have in my nginx config file:

location / {
if ($request_uri !~ "-f"){
        rewrite !\.(jpg|css|js|gif|png)$ public/ break;
}
rewrite !\.(jpg|css|js|gif|png)$ public/index.php?url=$1;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
        # Move to the @missing part when the file doesn't exist
        try_files $uri @missing;

        # Fix for server variables that behave differently under nginx/$
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # Include the standard fastcgi_params file included with ngingx
        include fastcgi_params;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_index index.php;


        # Pass to upstream PHP-FPM; This must match whater you name you$
        #fastcgi_pass phpfpm;
        fastcgi_pass 127.0.0.1:9000;
}

location @missing {
        rewrite ^(.*)$ public/index.php?url=$1 break;
}

However, when I hit /, I get a 403 Forbidden, but I can get to /public/index.php, thus the rewrite isn't working.

Any ideas on what I'm doing wrong?

© Server Fault or respective owner

Related posts about nginx

Related posts about .htaccess