【问题标题】:apache/nginx: .htaccess rewrite conversion mayhemapache/nginx:.htaccess 重写转换混乱
【发布时间】:2011-08-21 10:56:57
【问题描述】:

我正在尝试从 Apache 重写 .htaccess 规则以在 Nginx 服务器上使用。

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

这是我所拥有的工作,但如果有更好的方向,将不胜感激。我可以点击索引,它可以正常加载并浏览到资产文件夹,但更深的链接不起作用(PHP 程序正在从 URL 中提取变量来构建数据库查询)。我知道我很接近了.. 感谢您的任何回复。

location / {
  index index.php;
}
location /$ {
  rewrite ^/(.*)$ /index.php/$1 last;
}
location /index.php {
  root html;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
  include fastcgi_params;
}

【问题讨论】:

    标签: apache mod-rewrite url-rewriting nginx rewrite


    【解决方案1】:

    我认为这些规则会将不在 /assets 下的所有内容发送到 /index.php。我认为这会做你想要的。此外,它会将 root 和 index 指令移动到服务器中,这是它们应该被设置为所有位置的默认值的位置。

    # set server defaults directly in server context
    root /usr/share/nginx/html;
    index index.php;
    
    # location / is a fallback for any request that doesn't match
    # a more specific location
    location / {
      rewrite ^ /index.php$uri last;
    }
    
    # Serve content under /assets from disk
    location /assets {
    }
    
    # Extract path info and send /index.php for processing
    location /index.php {
      fastcgi_split_path_info ^(/index.php)(.*)
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
      fastcgi_pass 127.0.0.1:9000;
    }
    

    【讨论】:

    • 邪恶的,这很好用!现在唯一的问题是查询字符串没有正确地传递给 ajax 页面,它会导致整个 index.php 在 ajax 区域内重新加载。有什么想法吗?
    • 位置 / 的重写应该自动保留查询字符串。你编辑你的 fastcgi_params 文件了吗?它应该有这样一行:fastcgi_param QUERY_STRING $query_string;
    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 2014-06-30
    • 2016-08-09
    • 2014-02-18
    • 2011-04-16
    • 2021-04-04
    • 1970-01-01
    • 2017-06-06
    相关资源
    最近更新 更多