简体   繁体   English

重写URL段以获取参数nginx

[英]Rewrite url segments to get parameters nginx

I'm trying for a few hours now to solve this problem, that shouldn't be that much of a headache. 我现在尝试了几个小时来解决这个问题,这不应该那么令人头疼。

I need the correct code for a nginx server to acomplish this. 我需要Nginx服务器的正确代码来完成此任务。

I have an url like this: https://www.example.com/share/userid/file/ 我有这样的网址: https : //www.example.com/share/userid/file/

And I want to rewrite it to: https://www.example.com/share/?id=userid&key=file/ 我想将其重写为: https : //www.example.com/share/?id=userid&key=file/

I've tried it with several solutions here in stackoverflow but none of them have worked. 我在stackoverflow中尝试了几种解决方案,但是都没有用。

I've tried this: 我已经试过了:

rewrite ^/share/(.*)/(.*)$ /share/index.php?id=$1&key=$2 ;

That works if I place an index.php file there, however I can't do that cause /share/ is a permalink, not an actual folder (wordpress). 如果我将index.php文件放在那里,那行得通,但是我做不到,因为/ share /是一个永久链接,而不是一个实际的文件夹(wordpress)。 If I do this: 如果我这样做:

rewrite ^/share/(.*)/(.*)$ /share/?id=$1&key=$2 ;

I get a 404 nginx error. 我收到404 Nginx错误。

It also seems that ^/share/(. )/(. )$ is not actually triggering the rewrite, so that must be wrong although I'm looking at the nginx rewrite docs and looks ok. 这似乎也^ /股/()/()$实际上并没有引发重写,所以虽然我在看nginx的重写文档必须是错误的,看起来确定。

Any ideas? 有任何想法吗? I also tried with try_files with no success. 我也尝试过try_files,但没有成功。

Thank you 谢谢

UPDATE: 更新:

OK so this works 好的,这可行

rewrite ^(/share/)([0-9]+)/(.*)/$ https://$server_name/share/?id=$2&key=$3 last;

However for some reason if I do this it doesn't work: 但是由于某种原因,如果这样做,将无法正常工作:

rewrite ^(/share/)([0-9]+)/(.*)/$ /share/?id=$2&key=$3 last;

I had to add the $server_name to make it work, any one can explain me why? 我必须添加$ server_name使其起作用,任何人都可以解释我为什么?

I finally solved the issue myself: 我终于自己解决了这个问题:

rewrite ^(/share/)([0-9]+)/(.*)/$ https://$server_name/share/?id=$2&key=$3 last;

I used ([0-9]+) cause the first parameter is a integer 我使用([0-9]+)因为第一个参数是整数

However for some reason if I do this it doesn't work: 但是由于某种原因,如果这样做,将无法正常工作:

rewrite ^(/share/)([0-9]+)/(.*)/$ /share/?id=$2&key=$3 last;

I had to add 我必须添加

https://$server_name

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM