[英]migrate htaccess rewrite to nginx
我在 htaccess 上有以下重寫規則:
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^?]+?)(?:\.php)?\?lang=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]
該規則將“lang”參數重定向到子目錄: http://www.www.www/somepage?lang=en -> http://www.www.www/en/somepage
如何在 NGINX 上創建相同的規則?
您能否將以下規則放入您的 nginx 規則文件中。 我已經在路徑/etc/nginx/sites-enabled
中放置了一個名為 so_question 的文件,並在我的本地主機中的8081
上運行了該服務,這些工作非常好(通過curl
命令測試)。 順便說一句,我不是 nginx 方面的專家,我花了很多時間來學習解決這個問題:) 我希望這會有所幫助。
As per your shown htaccess this assumes you want to redirect a url from http://localhost:8181/test.php?lang=en_abc123_singhblabla
TO http://localhost:8181/en_abc123_singhblabla-test
(mentioned this in comments also inside Rule file )。
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.