【问题标题】:Converting .htaccess to nginx (mod_rewrite)将 .htaccess 转换为 nginx (mod_rewrite)
【发布时间】:2013-02-02 08:17:26
【问题描述】:

我的 apache 有以下 .htaccess 文件:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks    
# Options +SymLinksIfOwnerMatch
  RewriteEngine On
  RewriteBase /
  RewriteRule ^$          index.php       [L]
  RewriteCond %{REQUEST_FILENAME}         !-f
  RewriteCond %{REQUEST_FILENAME}         !-d
  RewriteRule (.*)        index.php?page=$1  [QSA,L]
</IfModule>

突然间我不得不将我的网络服务器更改为 nginx,我不知道为什么,但是 mod 重写不起作用。

我使用了一个在线“转换器”来转换它,所以我得到了以下内容:

location / {
  rewrite ^/$ /         index.php       break;
  if ($request_filename ~         !-f){
    rewrite ^(.*)$ /       index.php?page=$1   break;
  }
}

你能帮我看看有什么问题吗?

提前致谢, 马塞尔

【问题讨论】:

  • 哪个白痴关闭了这个?大多数网站都需要在从 Apache 而不是 nginx 迁移时更新 mod 重写规则。

标签: apache .htaccess mod-rewrite nginx


【解决方案1】:

http://web.archive.org/web/20180812021847/https://blog.martinfjordvald.com/2011/02/nginx-primer-2-from-apache-to-nginx/

一切都在里面。不再需要 .htaccess,不再需要复杂的规则使用 try_files。

编辑:如果不是很明显,请不要相信在线转换器。

【讨论】:

    【解决方案2】:

    $ sudo vim /etc/nginx/sites-available/default

      location / {
            try_files $uri $uri/ =404;
        }
    

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

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 2016-12-13
      • 2012-07-19
      • 2021-01-01
      • 2018-07-10
      • 2017-08-02
      • 2011-05-07
      • 1970-01-01
      相关资源
      最近更新 更多