将htaccess转换为nginx重写
问题描述:
.htaccess访问nginx重写转换帮助
.htaccess to nginx rewrite convertion help
RewriteEngine On
RewriteBase /
RewriteRule ^c-(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
答
假设您拥有有效的PHP实现,请尝试:
Assuming you have a working PHP implementation, try:
location / {
rewrite ^/c-(.*)$ /catpost.php?id=$1 last;
rewrite ^/a-(.*)-(.*)$ /archives.php?month=$1&year=$2 last;
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^/(.*)$ /viewpost.php?id=$1 last;
}
要记住的主要事情是nginx
URI以开头的/
开头.
The main thing to remember is that nginx
URIs begin with a leading /
.
前两个重写可能在location
块内,也可能不在-取决于您拥有的其他location
块.有关详细信息,请参见本文档.
The first two rewrites may be inside the location
block or not - depending on what other location
blocks you have. See this document for details.
基本上,条件重写是try_files
设计的目的.我们使用命名位置来丢失前导/
.有关详细信息,请参见此文档.
The conditional rewrite is basically what try_files
is designed to do. We use a named location to lose the leading /
. See this document for details.