.htaccess 为nginx重写

.htaccess 为nginx重写,.htaccess,url-rewriting,nginx,.htaccess,Url Rewriting,Nginx,您好,我正在尝试将这些文件重写为nginx格式,但无法取得任何进展。你能帮我为nginx重写这些吗 # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !public/ RewriteCond %{DOCUMENT

您好,我正在尝试将这些文件重写为nginx格式,但无法取得任何进展。你能帮我为nginx重写这些吗

# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.|\?)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /render to the image parser
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]

# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]
我试过这个,但没用

location  public/ {
}

location ~ (\.|\?) {
}

location ~ (.*)/$ {
}

location  public/ {
}

location / {
  if (!-e $request_filename){
    rewrite (.+)\.(html|json|xml|atom|rss|rdf|txt)$ /$1/ break;
  }
  if (!-e $request_filename){
    rewrite ([^/]+)$ /$1/ break;
  }
   if ($request_uri ~ "render/"){
    rewrite ^/render/. /app/parsers/slir/ break;
  }
  if ($request_uri ~ "/app/$"){
    rewrite ^/app/ /index.php break;
  }
   if (!-e $request_filename){
    rewrite ^/(.*)/$ /index.php?/$1/ break;
  }
  if (!-e $request_filename){
     rewrite ^(.+)$ /public/$1 break;
  }
}

当我尝试此操作并输入我的url时。它会下载一个文件。

我不确定它是否与您的问题完全匹配,但这应该会给您一些帮助,并为下一步的研究提供起点。还有一个建议-nginx的工作原理与Apache稍有不同,所以不要尝试使用类似if的nginx-e$request\u文件名-这是一个非常糟糕的主意,这里有些东西:


非常感谢emka,但您的代码仅显示404未找到页面。它不起作用。请尝试逐个使用位置指令。可能存在一些冲突,而且如果没有有效的根指令try\u文件将无法正确工作,它将从整体上被切断。我只是试着给你一个例子,如何将apache规则转换为nginx规则。这不是复制/粘贴,我什么都不懂。天哪,我讨厌这个。我不是程序员。我是一个三维造型艺术家。我只想在我的公文包网站上使用更干净的URL。
# It's your first rule:
# If request location match this regex then
# try file as request, if not exist
# try a folder matching * or
# return with 404
location ~ (.+)\.(html|json|xml|atom|rss|rdf|txt)$ {
   try_files $uri $1/ =404;
}

# It's firts part of your second rule:
# If request match this regex 
# try file as request if not exist
# try file with added slash or
# return with 404
location ~ !(\.|\?) {
   try_files $uri $uri/ =404
}

# It's second part of your second rule:
# If request match this regex
# try file as request if not exist
# try file with added slash or
# return with 404
location ~ !(.*)/$ {
   try_files $uri $uri/ =404
}

# It your third rule:
# Rewrite /render to /app/parsers/slir
location = /render {
   rewrite .* /app/parsers/slir break;
}

# It's first part of your forth rule:
# If request location match this request then use /index.php
# or return with 404
location ~ ^/app/$ {
   try_files /index.php =404;
}

# It's second part of your forth rule:
# If request location match regex try to use it,
# if not try /index.php?/$1/ location
# or return with 404
location ~ ^(.*)/$ {
   try_files $uri /index.php?/$1/ =404;
}

# It's your fifth rule:
# Try file with match this reqex, if not try location /public/$1
# or return with 404
location ~ ^/(.+)$ {
   try_files $uri /public/$1 =404;
}

# It's a global rule
# For all request try file as it is
# or return with 404
location / {
   try_files $uri =404;
}