【问题标题】:migrate htaccess rewrite to nginx将 htaccess 重写迁移到 nginx
【发布时间】:2021-03-26 22:11:33
【问题描述】:
【问题讨论】:
标签:
.htaccess
nginx
redirect
mod-rewrite
url-rewriting
【解决方案1】:
您能否将以下规则放入您的 nginx 规则文件中。我在路径/etc/nginx/sites-enabled 中放置了一个名为so_question 的文件,并在我的本地主机中的8081 上运行了该服务,这些都运行良好(通过curl 命令测试)。顺便说一句,我不是 nginx 方面的专家,我花了很多时间来学习解决这个问题 :) 我希望这会有所帮助。
根据您显示的 htaccess,这假设您要将一个 URL 从 http://localhost:8181/test.php?lang=en_abc123_singhblabla 重定向到 http://localhost:8181/en_abc123_singhblabla-test(在规则文件中的 cmets 中也提到了这一点)。
cat so_question
server {
listen 8181;
listen [::]:8181 ipv6only=on default_server;
server_name localhost;
##Actual redirect rule which redirects from a URL eg-->
##http://localhost:8181/test.php?lang=en_abc123_singhblabla -->
##http://localhost:8181/en_abc123_singhblabla-test
if ($request_uri ~ "^/([^.]*)\.php\?lang=(.*)") {
set $original_path $1;
set $args1 $2;
set $args "";
rewrite ^ "$scheme://$host/${args1}-${original_path}" permanent;
}
}