首页 > 解决方案 > 如何将以下简单的 Nginx 重写条件和规则转换为 Apache .htaccess

问题描述

我想将以下 Ngnix 重写规则和条件转换为 Apache .htaccess,但我无法成功。

location /app/ {
  if (!-e $request_filename) {
    rewrite ^ /app/index.html break;
  }
}

location ~ /app/account/(.*?)$ {
  try_files $uri $uri/ /index.php?$query_string;
}

location ~ /app/pay/(.*?)$ {
  try_files $uri $uri/ /index.php?$query_string;
}

标签: apache.htaccessmod-rewritewebserver

解决方案


您能否仅根据您显示的代码尝试以下操作。此外,我不是 nginx 专家,但我在这里尽了最大努力 :) 请确保在测试 URL 之前清除浏览器缓存。

显然,如果您可以为我们提供 URL 示例,其中包含您想要重写/重定向的从哪个 url 到哪个 url 的逻辑,那么它会更容易更好地理解它,并且对测试也有帮助。

RewriteEngine ON
##First rule here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /app/index.html [L]

##Second rule here....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/account/(.*)/?$ index.php?$1 [L]

##Third rule here....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/pay/(.*)/?$  index.php?$1 [L]

推荐阅读