且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 htaccess 转换为 nginx 重写

更新时间:2023-02-24 12:38:31

假设您有一个有效的 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.