You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx Rewrite不跳转问题:URL重定向配置求助

Fixing Nginx URL Redirect for /info/schoolName/detail to /school-info?name=schoolName

Let's break down why your current configuration isn't triggering the redirect, then fix it properly.

The Problem with Your Current Config

Your second location rule:

location ~* ^/(info|info/)$ {
    include /etc/nginx/servers/platform/shared/headers_proxy.conf;
    rewrite ^/info/(.?)/(.*)$ /school-info?school=$1 permanent;
    proxy_pass $backend_cms;
}

has a critical issue: the regex ^/(info|info/)$ only matches exact paths /info or /info/. It won't touch URLs like /info/byu/detail because those have extra segments after /info/. That's why your redirect never fires.

The Correct Configuration

Replace that problematic location block with one that targets the full path pattern you need, and captures the schoolName segment properly:

# Handle redirects for /info/schoolName/any-path-here
location ~ ^/info/([^/]+)/ {
    # Capture the schoolName (anything between /info/ and the next /), ignore everything after
    rewrite ^/info/([^/]+)/.*$ /school-info?name=$1 permanent;
    # No proxy_pass needed here—once redirected, the new URL will hit your existing school-info location
}

# Keep your existing school-info proxy configuration as-is
location ~ ^/(school-info)(?:/(.*))?$ {
    include /etc/nginx/servers/platform/shared/headers_proxy.conf;
    proxy_set_header Host $backend_netlify_main;
    proxy_ssl_server_name on;
    proxy_pass https://$backend_netlify_main/$1/$2;
}

Key Details Explained

  • Regex ^/info/([^/]+)/: This matches any URL starting with /info/, followed by a segment of non-slash characters (your schoolName), then another slash. The ([^/]+) captures that school name into a group we can use in the rewrite.
  • Rewrite Rule: ^/info/([^/]+)/.*$ matches the entire path, replacing it with /school-info?name=$1 where $1 is the captured school name. The .*$ ensures we ignore everything after the school name segment, just like you wanted.
  • Redirect Type: permanent sends a 301 redirect (good for SEO and permanent changes). If you need a temporary redirect, swap it for redirect (302).

Quick Testing Steps

  1. Validate your Nginx config syntax with:
    nginx -t
    
  2. If no errors, reload the config to apply changes:
    nginx -s reload
    
  3. Test accessing https://test.com/info/byu/detail—it should now redirect to https://test.com/school-info?name=byu.

内容的提问来源于stack exchange,提问作者legendary_rob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 18:42:58