php - CodeIgniter 的 Nginx 重写规则

标签 php apache .htaccess codeigniter nginx

这是英文规则:

Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your index.php file.

我有一个 .htaccess在 Apache 服务器上正常工作的文件:

RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

此规则的一些正确结果:

example.com = example.com/index.php

example.com/index.php/welcome = example.com/welcome

example.com/assets/css/main.css != example.com/index.php/assets/css/main.css

我尝试了一些工具将 htaccess 规则转换为 nginx 规则,但都不正确。

通过http://winginx.com/htaccess (缺少 Assets 文件夹的异常(exception)规则...):

location / { rewrite ^(.*)$ /index.php/$1 break; }

通过http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ ($1 值错误):

if ($1 !~ "^(index.php|assets|files|robots.txt)"){
    set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
    rewrite ^/(.*)$ /index.php/$1 last;
}

我该如何解决这个问题?调试规则真的很难。

到目前为止,这是我的 nginx 配置:

server {
    listen       80;
    server_name  www.example.com example.com;

    location / {
        try_files $uri $uri/ /index.php?/$request_uri;
    }

    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

最佳答案

您可以将其添加到您的配置中:

location ~* ^/(assets|files|robots\.txt) { }

这将与您的 location/ 规则一起正常工作。

您的配置还需要添加根文档和默认索引文件。

...
root /ftp/wardrobe;
index index.php index.html index.htm;

location / {
    try_files $uri $uri/ /index.php?/$request_uri;
}

location ~* ^/(assets|files|robots\.txt) { }
...

关于php - CodeIgniter 的 Nginx 重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19347306/

相关文章:

java - 从 PHP 脚本调用 Java 时出现问题

PHP电子邮件管道

java - Eclipse : The import org. apache无法解析

java - Apache cxf 服务器异步请求处理

php - 通过 CURL 登录 htpasswd 并将 session 保留在浏览器中

如果用户名/电子邮件错误,PHP 不会执行任何操作

php - PHP 中的多个扩展

java - mvn 测试不适用于 Apache Poi

.htaccess - 使用 .htaccess 以 root 身份动态加载子域文件夹

php - 如何创建漂亮的网址