【问题标题】:Convert htaccess to nginx rewrite将 htaccess 转换为 nginx 重写
【发布时间】:2017-06-06 08:57:04
【问题描述】:
.htaccess 到 nginx 重写转换帮助
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]
【问题讨论】:
标签:
apache
.htaccess
nginx
【解决方案1】:
假设您有一个有效的 PHP 实现,请尝试:
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 以 / 开头。
前两个重写可能在 location 块内或不在 - 取决于您拥有的其他 location 块。详情请见this document。
条件重写基本上是try_files 的设计目的。我们使用命名位置来丢失领先的/。详情请见this document。