将URL重写为Apache和Nginx上的查询字符串

use*_*011 10 regex apache mod-rewrite nginx url-rewriting

我正在尝试在两个单独的服务器上设置一些路径重写,一个在Apache上使用mod-rewrite,另一个在Nginx上使用HttpRewriteModule.我不认为我想做任何太复杂的事情,但我的正则表达能力有点缺乏,我真的可以使用一些帮助.

具体来说,我正在尝试将格式化的URL转换为查询字符串,以便格式化如下链接:

http://www.server.com/location/
Run Code Online (Sandbox Code Playgroud)

会指出这个:

http://www.server.com/subdirectory/index.php?content=location
Run Code Online (Sandbox Code Playgroud)

格式化URL末尾的任何额外内容都应附加到查询字符串中的"content"参数,因此:

http://www.server.com/location/x/y/z
Run Code Online (Sandbox Code Playgroud)

应该指出这个:

http://www.server.com/subdirectory/index.php?content=location/x/y/z
Run Code Online (Sandbox Code Playgroud)

我很确定根据我所做的研究,使用Apache mod-rewrite和Nginx HttpRewriteModule应该可以实现这一点,但是我看不到它能够正常工作.如果有人能给我一些关于如何将这些设置中的一个或两个的表达式组合在一起的指示,我将非常感激.谢谢!

Jos*_*ipt 5

在nginx中,您在重写指令中匹配"/ location",捕获变量$ 1中的尾部字符串并将其附加到替换字符串.

server {
...
rewrite ^/location(.*)$ /subdirectory/index.php?content=location$1 break;
...
}
Run Code Online (Sandbox Code Playgroud)

在Apache的httpd.conf中,这看起来很相似:

RewriteEngine On
RewriteRule ^/location(.*)$ /subdirectory/index.php?content=location$1 [L]
Run Code Online (Sandbox Code Playgroud)

请看一下本页末尾的示例:https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html


pog*_*ogo 3

搜索字符串:(.+)/location/(.*)$

替换字符串:$1/subdirectory/index.php?content=location/$2