将 HTACCESS 重写规则转换为 Nginx

问题描述 投票:0回答:1

我在 .htaccess 文件中有以下重写规则。我怎样才能让它在 Nginx 中工作?

RewriteRule ^profile/([^/\.]+)/?$ profile.php?id=$1 [L]

尝试过这个,但没有成功:

rewrite profile.php?id=$1 ^profile/([0-9]+)/?$ break;
nginx webserver
1个回答
0
投票

你的Nginx rewrite参数被颠倒了。

假设您的

.htaccess
文件位于文档根目录中,请尝试:

rewrite ^/profile/([0-9]+)/?$ /profile.php?id=$1 last;
  • 所有 Nginx URL 都以前导
    /
    开头,因此正则表达式和替换都应分别以
    ^/
    /
    开头。
  • 替换的是 PHP 脚本,因此应该使用
    last
    标志来让 Nginx 搜索适当的
    location
    块来处理 PHP 脚本。
  • 如果
    .htaccess
    文件位于子目录中,它将应用于以该子目录为前缀的 URL,因此正则表达式和替换都应以该子目录为前缀。
© www.soinside.com 2019 - 2024. All rights reserved.