Nginx中的htaccess重写规则:设置重写路径

Nginx中的htaccess重写规则:设置重写路径,nginx,rewrite,Nginx,Rewrite,我有一个htaccess文件,我正试图转换成一个nignx配置文件 这是我的htaccess文件 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?ur

我有一个htaccess文件,我正试图转换成一个nignx配置文件

这是我的htaccess文件

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
以及我的nginx配置文件中的规则:

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;
}
然而,当我点击/时,我得到了一个403禁止,但我可以访问/public/index.php,因此重写不起作用

有没有关于我做错了什么的想法?

您的
if($request\u uri!~“-f”)
-语句在nginx中没有按您希望的方式进行解析。您没有检查文件是否存在,而是根据否定的正则表达式
-f
进行加工。要在nginx中检查if中是否存在文件,请使用
if(-f$request\u filename)

有关nginx
if
语句的完整详细信息,请参阅

通常,您希望替换common.htaccess节:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule    <regex>    <target>
location ~* <regex> { try_files $uri $uri/ <target>;}
location / {
  location ~* \.(jpg|css|js|gif|png)$ { try_files $uri $uri/ /public/; }
  location !~* (.*)\.(jpg|css|js|gif|png)$ { 
    try_files $uri $uri/ /public/index.php?url=$1; 
  }
}