将 Apache 重写规则转换为 Nginx
Convert Apache rewrite rules to Nginx
如何将这些规则从 .htaccess (apache) 转换为 Nginx conf?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ .html
RewriteRule ^([^/]+)/([^/]+)/$ /-.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ // [R=301,L]
我使用这些规则重写请求,例如:
/someurl1/ => someurl.html (/company/ to /company.html
/someurl1/someurl2/ => someurl-someurl2.html (/projects/3/ to /projects-3.html)
我已经试过了(没用):
if (!-e $request_filename) {
rewrite ^/([^/]+)/$ /.html;
rewrite ^/([^/]+)/([^/]+)/$ /-.html;
}
try_files $uri $uri/ =404;
我哪里错了?
我已经测试了以下内容并且似乎有效:
location / {
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^/([^/]+)/$ /.html last;
rewrite ^/([^/]+)/([^/]+)/$ /-.html last;
return 404;
}
RewriteCond %{REQUEST_FILENAME} !-f
测试实际上是由try_files
指令完成的。
详情见this。
如何将这些规则从 .htaccess (apache) 转换为 Nginx conf?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ .html
RewriteRule ^([^/]+)/([^/]+)/$ /-.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ // [R=301,L]
我使用这些规则重写请求,例如:
/someurl1/ => someurl.html (/company/ to /company.html
/someurl1/someurl2/ => someurl-someurl2.html (/projects/3/ to /projects-3.html)
我已经试过了(没用):
if (!-e $request_filename) {
rewrite ^/([^/]+)/$ /.html;
rewrite ^/([^/]+)/([^/]+)/$ /-.html;
}
try_files $uri $uri/ =404;
我哪里错了?
我已经测试了以下内容并且似乎有效:
location / {
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^/([^/]+)/$ /.html last;
rewrite ^/([^/]+)/([^/]+)/$ /-.html last;
return 404;
}
RewriteCond %{REQUEST_FILENAME} !-f
测试实际上是由try_files
指令完成的。
详情见this。