![使用 Nginx 将 /a/1/2 重写为 /a.php](https://linux22.com/image/669732/%E4%BD%BF%E7%94%A8%20Nginx%20%E5%B0%86%20%2Fa%2F1%2F2%20%E9%87%8D%E5%86%99%E4%B8%BA%20%2Fa.php.png)
我正在接管一个非常旧的应用程序并将其从 apache 迁移到 nginx。
我目前拥有的。这有效:
# catch specific paths, rewrite to first segment
# /a/1/2 => a.php
# /b/3/4 => b.php
# /c/5/6 => c.php
rewrite ^/(a|b|c)(/.*)?$ /$1.php last;
# all other paths go to index.php
location / {
try_files $uri /index.php$args;
}
但是,在我的实际文件中,(a|b|c)
长度约为 100 个段。我想要做的是编写一个正则表达式来替换它
我试过这个
# rewrite first segment of path as /a/1/2 => a.php
location ~* ^/([a-z_]+)(/.*)?$ {
try_files $uri /$1.php$args /index.php$args;
}
# all other paths to index.php
location / {
try_files $uri /index.php$args;
}
但它不起作用:{
问题:/a/1/2
当我实际访问nginx 时下载文件a.php
,而不是提供它