帮助与Apache到Nginx重写规则

我需要帮助将一些Apache .htaccess规则转换为nginx规则。 希望有更多经验的人能帮助我。 这是我的规则:

1

RewriteEngine on RewriteCond %{HTTP_HOST} ^www.url.ba [NC] RewriteRule ^(.*)$ http://url.ba/$1 [R=301,L] RewriteRule ^api\.php /index.php?c=api&m=index&%{QUERY_STRING} [L] RewriteRule ^contact\.php /index.php?c=contact&m=index&%{QUERY_STRING} [L] RewriteRule ^([a-zA-Z0-9]{4,25})$ /index.php?c=api&m=check&hash=$1 [L] RewriteCond $1 !^(index\.php|images|css|script|ZeroClipboard\.swf) RewriteRule ^(.*)$ /index.php/$1 [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+) / [R=301,L] 

2

  RewriteEngine on RewriteCond %{HTTP_HOST} ^www.img.ba.ba$ [NC] RewriteRule ^(.*)$ http://img.ba/$1 [R=301,L] RewriteCond $1 !(\/protected)$ RewriteRule ^(.*)\/protected$ /protected.php?hash=$1 [L] RewriteCond $1 !(thumb\.php)$ RewriteRule ^(.*)\/thumb$ /thumb.php?hash=$1 [L] RewriteCond $1 !^(index\.php|thumb\.php|upload\.php|contact\.php|protected\.php|api\.php|password\.php|favicon\.ico|images|css|script) RewriteRule ^(.*)$ /index.php?hash=$1 [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+) / [R=301,L] 

既然你已经准备好了规则,那很简单。 这是指导原则

  • 阅读重写文档
  • 读取核心variables ( %{HTTP_HOST}变为$host
  • RewriteCond将会变成if
  • 除了variables差异和正则expression式的用法之外, RewriteRules几乎是一样的。 此外,标志是完全写入breakpermanent等。

重写页面上有很多例子。

大多数的Apache重写条件应该变成如果块。 那些匹配HTTP_HOST的条件应该被转换成服务器块,匹配URI或者请求文件名的那些条件应该用位置块代替,并且那些testing存在的文件应该被replace为try_files。 如果没有必要,请不要使用。 假设所有的dynamic请求都应该通过php,我认为这些规则的正确翻译是:

 server { server_name www.url.ba; rewrite ^ http://url.ba$request_uri? permanent; } server { server_name url.ba; root /path/to/root; location = /api.php { rewrite ^ /index.php?c=api&m=index; } location = /contact.php { rewrite ^ /index.php?c=contact&m=index; } location ~ "/([a-zA-Z0-9]{4,25})$" /index.php?c=api&m=check&hash=$1?; } location /index.php { fastcgi_split_path_info (/index.php)(.*); include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_pass your_backend; } location /images { try_files $uri @home; } location /css { try_files $uri @home; } location /script { try_files $uri @home; } location /ZeroClipboard.swf { try_files $uri @home; } location @home { rewrite ^ / permanent; } location / { rewrite ^ /index.php$uri; } } server { # Did you really mean .ba.ba? server_name www.img.ba.ba; rewrite ^ http://img.ba$request_uri? permanent; } server { server_name img.ba; # I'm not positive that these translations are correct, # I'm guessing that the repeated $1 is always the requested # uri in the apache rules # The (?<variable> capture syntax is 0.8.25+. If you're # running an older version, the easiest way will be to # repeat the location regex in the rewrite and capture $1 location ~ ^/(?<hash>.*)/protected$ { rewrite ^ /protected.php?hash=$hash; } location ~ ^/(?<hash>.*)/thumb\.php$ { rewrite ^ /thumb.php?hash=$hash; } location ~ ~/(index|thumb|upload|contact|protected|api|password)\.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass your_backend; } location /images { try_files $uri @home; } location /css { try_files $uri @home; } location /script { try_files $uri @home; } location @home { rewrite ^ / permanent; } location / { rewrite ^/(.*) /index.php?hash=$1; } }