2017-04-10 160 views
0

我正在将应用程序从Apache移动到nginx,并且我被重写部分卡住了。将htaccess重写转换为nginx重写

在我的Apache .htaccess中,我有这个。基本上,将一切,包括查询变种以我的index.php

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/?$ index.php?p=$1 [QSA,L] 

里面的index.php我有一个非常基本的部分,打破下来,所以我可以路线访问者,他们需要去哪里,也处理查询变种

if (isset($_GET['p'])) { 
    $page = explode('/', $_GET['p']); 
} else { 
    $page = NULL; 
} 

而且在脚本后

if ($page) { 
    } if ($page[0] == "stream") { 
     if (isset($page[1])) { 
      // Blah 
     } 
    } 
} 

同样在各种主题的PHP文件,我已经抓住我的URL与$_GET['id']或瓦尔做各种功能。

例如http://example.com/verify?code=blah

该URI进入index.php,验证被发现是一个名为verify.php的主题文件,在verify.php内抓取代码url var。

我正在寻找与nginx一起复制,并陷入困境。我已经试过以下:

 location/{ 
#    try_files $uri $uri/ =404; 
       rewrite ^(.*)/?$ index.php?p=$1 last; 
#    try_files $uri $uri/ /index.php?p=$args; 
#    try_files $uri $uri/ /index.php?p=$query_string; 
     } 

我甚至试过example from this answer,但仍然没有工作。

在这里,我试图把它应用到我的设置:

location/{ 
     rewrite ^(.*)/?$ index.php?p=$1 last; 

     try_files $uri $uri/ @rewrite; 
} 
location @rewrite { 
     rewrite ^(.*)/?$ index.php?p=$1 last; 
} 

的注释的部分是我的试验和错误。关于如何将Apache复制到nginx格式的任何见解?

+0

您可以将其标记为重复。起初那个引用的答案并没有解决我的问题,但最终确实如此。感谢 – Pat

回答

0

你试过this的网站吗?

location/{ 
    if (!-e $request_filename){ 
     rewrite ^/(.*)/?$ /index.php?p=$1 break; 
    } 
} 
+0

有趣的是,它希望我下载页面而不是加载页面。例如,如果我访问http://example.com/about,它希望我下载“about”。如果我下载它,内容是我的原始php'index.php'文件 – Pat

0

我不想回答我的问题,但在我的问题的answer referenced实际上是正确的答案。我希望这将有助于未来的人。

我有点太急于尝试这个答案,并且因为上面的代码片段失败了,因为我把我的重写放在了两次。工作解决方案只是把我的重写只写在重写位置。

location/{ 
      try_files $uri $uri/ @rewrite; 
    } 

    location @rewrite { 
      rewrite ^/(.*)/?$ /index.php?p=$1 last; 
    } 

这(迄今)已成功地工作。它将URL重写为特定的php主题页面,主题页面可以访问查询变量。