重写代码从htaccess到nginxconfiguration?

我有问题与执行我的重写代码从htaccess文件到nginxconfiguration。 我已经尝试生成器: http : //winginx.com/htaccess生成我的重写代码。

我的nginxconfiguration代码:

server { listen 80; server_name example.com; return 301 $scheme://www.example.com$request_uri; } server { listen 80; root /usr/share/nginx/www; index index.php; server_name www.example.com; error_page 404 http://www.example.com/404.php; autoindex off; error_log /usr/share/nginx/www/nginx_error.log warn; location / { rewrite ^([^\.]*)$ /$1.php; } location = / { rewrite ^ /index.php; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; } } 

我想从我的.htaccess实现这个:

 RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ admin/index.php?hotelname=$1&do=$2 [QSA] RewriteRule ^(([A-Za-z0-9-/]+)+)$ admin/index.php?hotelname=$1 [L] 

从工具生成的代码:

 location / { rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2; rewrite ^/(([A-Za-z0-9-/]+)+)$ /admin/index.php?hotelname=$1 break; } 

我已经尝试实现这个最后一行代码到我的位置块,但不工作..我会非常greateful每个意见! 问候Makkeh

盲目的转换将是

 rewrite ^([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ admin/index.php?hotelname=$1&do=$2&$query_string last; rewrite ^(([A-Za-z0-9-/]+)+)$ admin/index.php?hotelname=$1 last; 

但是,如果我更多地理解这个问题以产生更好的重写,我会更喜欢。

我什么时候知道URL是否应该传递给/admin ,给我一个真实的后端和前端的URI。

通常情况下,使用nginx的思维方式,重写可以更好地在nginx中进行管理。 而这种新思维方式更多的是基于try_file

所以你可以尝试类似的东西(未经测试):

 location ^~ "/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)" { try_files $uri admin/index.php?hotelname=$1&do=$2&$args; } location ^~ "(([A-Za-z0-9-/]+)+)" { try_files $uri /admin/index.php?hotelname=$1; } location = / { rewrite ^ /index.php; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; } 

如果直接访问给定的$uri不应该发生,那么从try_files中删除该部分。 现在我也不确定你的第二个正则表达式(([A-Za-z0-9-/]+)+) ,为什么不使用:

 location ^~ "/([A-Za-z0-9-/])+" 

要么

 location ^~ "/([A-Za-z0-9-])+/" 

所以也许有些东西我看不到,即使在你的Apache重写。