Мне нужна помощь в преобразовании некоторых правил 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]
Поскольку у вас есть готовые правила, это легко. Вот рекомендации
%{HTTP_HOST}
становится $host
, например)RewriteCond
будет превращен в if
блокиRewriteRules
почти такие же, за исключением различий переменных и использования регулярных выражений. Также флаги записываются полностью как break
, permanent
и так далее.На странице перезаписи есть много примеров.
Большинство условий перезаписи apache должны не превратиться в блоки if. Те условия, которые соответствуют HTTP_HOST, должны быть преобразованы в серверные блоки, те, которые соответствуют uri или имени файла запроса, должны быть заменены блоками местоположения, а те, которые проверяют существование файлов, должны быть заменены на try_files. Пожалуйста, не используйте блоки if там, где они не нужны. Предполагая, что все динамические запросы должны проходить через 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; }
}