本文介绍了将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.

这篇关于将htaccess转换为nginx重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:13