如何用Nginx的rewrite语法改写apache

以下是apache的形式,想换成nginx的rewite,请教各位大佬帮助,谢谢

RewriteEngine on
RewriteCond %{http_host} ^a.com [NC]
RewriteRule ^(.*)$ http://www.a.com$1 [R=301,L]

RewriteRule ^/track/(.*)$ /b/b/r.php?param=$1 [L]

RewriteCond  %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/((?!\.).)*(?<!\/)$      $0/ [R=301,L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI}   !^/(public|admin)/{0,1}
RewriteRule ^/  /index.php [L]
阅读 3.9k
4 个回答

大佬来帮你了

location ~ ^/(public|admin)/\{0,1} {
  try_files $uri $uri/ /index.php?$query_string;
}

location / {
  if ($http_host ~* "^a.com"){
    rewrite ^(.*)$ http://www.a.com$1 redirect;
  }
  rewrite ^/((?!.).)*(?<!/)$ /$arg_0/ redirect;
}

location /track {
  rewrite ^/track/(.*)$ /b/b/r.php?param=$1 break;
}

@Albert
多谢回答,但好像不能用

location ~ ^/(public|admin)/{0,1}" {
try_files $uri $uri/ /index.php?$query_string;
}
这个双引号是什么意思?

新手上路,请多包涵
location ~ ^/(public|admin)/\{0,1} {
  try_files $uri $uri/ /index.php?$query_string;
}

location / {
  if ($http_host ~* "^a.com"){
    rewrite ^(.*)$ http://www.a.com$1 redirect;
  }
  rewrite ^/((?!.).)*(?<!/)$ /$arg_0/ redirect;
}

location /track {
  rewrite ^/track/(.*)$ /b/b/r.php?param=$1 break;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏