我需要帮助将一些 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]
答案1
答案2
大多数 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; }
}