转换.htaccess到nginx是打破我的应用程序

我一整天都在努力工作,而且还没有运气。 我用一个在线的.htaccessconfiguration转换器,所以我不认为它正确地转换一切。

以下是我的.htaccess文件

RewriteCond %{QUERY_STRING} ^$ RewriteRule ^((.)?)$ index.php?p=home [L] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond $1 !^(\#(.)*|\?(.)*|\.htaccess(.)*|\.htaccess\.back(.)*|.idea\/(.)*|.svn\/(.)*|admin\.php(.)*|content\/(.)*|download\.php(.)*|ecc\/(.)*|images\/(.)*|index\.php(.)*|install\/(.)*|login\.php(.)*|readme\.txt(.)*|robots\.txt(.)*) RewriteRule ^(.+)$ index.php?url=$1&%{QUERY_STRING} [L] 

和我的转换nginxconfiguration

 try_files $uri $uri/ /index.php?url=$uri&$args; location / { if ($query_string ~ "^$"){ rewrite ^/((.)?)$ /index.php?p=home break; } if (-e $request_filename){ rewrite ^(.*)$ /$1 break; } rewrite ^(.+)$ /index.php?url=$1&$query_string break; } location ~* (^(?!(?:(?!(php|inc)).)*/uploads/).*?(php)) { try_files $uri = 404; fastcgi_split_path_info ^(.+.php)(.*)$; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; } 

发生什么事情是,当我尝试去像/admin.php?p=settings&group=3这样的url,它redirect我(因为我相信它不带有查询string)到login。 当我转到像/index.php?p=login/index.php?p=signup这样的东西时,它可以正常工作。

有人可以帮助我吗? -约翰

我认为你所犯的错误是试图将htaccess翻译成nginx conf。 两种configuration风格是完全不同的。 更好的方法是尝试在nginx中实现你的htaccess的逻辑

所以,你htaccess做到以下几点:

  1. 当请求不包含path(只是域的根)时,将请求重写为/index.php?p=home
  2. 当请求是一个文件,只是返回
  3. 第三个有点难。 你想重写index.php?url =&的请求,除了一些你想要保护的请求,比如'.svn'。

所以,这样的事情可能(未经testing):

 # Block some bad requests [3], only included a few here to get the idea location ~ (\.svn|\.htaccess|\.last|robots\.txt) { deny all; } location / { # see if a file or dir corresponding to the request exists and use that [2] try_files $uri $uri/ @rewrite; } # Rewrites [1] + [3] location @rewrite { rewrite ^$ /index.php?p=home break; rewrite $(.*)$ /index.php?url=$1; } location ~ \.php$ { try_files $uri $uri/ /index.php?url=$uri&$args; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; } 

正如我所说,未经testing,但这是更多的nginx-ish。