2016-04-26 167 views
0

我会尽量简短。我有以下的nginx重写网址:nginx重写&参数全url和文件扩展名

rewrite ^/(.*)?$ /index.php?completeURL=$1 last; 

我要像一个网址:

http://mywebsite.com/http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1 

要求:

http://mywebsite.com/index.php?completeURL=http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1 

目前nginx的规则有问题。 示例:如果参数包含.php扩展名,他会在我的服务器上查找该文件。

例子:http://mywebsite.com/dir1/dirx/article.php

如何解决在您看来这个问题呢?


UPDATE: 这里nginx的配置(和重写)文件:

回答

0

钍最简单的解决方案(假定服务器除了服务index.php之外不做其他任何事情)将删除通常的location ~ \.php$块,并在与fastcgi_pass相同的块中执行rewrite ... break;。有许多的实现这一点,包括方式:

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

另一种策略是只有本地文件不存在进行重写,但你需要确保.php文件过测试。例如:

location/{ 
    try_files $uri $uri/ @rewrite; 
} 
location ~ \.php$ { 
    try_files $uri @rewrite; 
    fastcgi_pass ... 
    ... 
} 
location @rewrite { 
    rewrite ^/(.*)?$ /index.php?completeURL=$1 last; 
}   
+0

我想你的第二个解决方案,但我得到这个错误: '2016年4月26日22时21分05秒[错误] 10591#0:* 1846重写或内部重定向循环,同时重定向到命名位置“@rewrites”,客户端:xx.xx.xx.xx,服务器:website.com,请求:“GET /http://www.website.com/dfgdfgghgh.cfm HTTP/2.0”,主机:“ website.com“' – Ivan

+0

如果/index.php不存在,它将循环。 'root'是否正确定义? –

+0

嗨@Richard Smit。我更新了我的帖子。我已经包含了完整的nginx配置文件。试着给我们看看。 – Ivan