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 (yourschoolName), 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=$1where$1is the captured school name. The.*$ensures we ignore everything after the school name segment, just like you wanted. - Redirect Type:
permanentsends a 301 redirect (good for SEO and permanent changes). If you need a temporary redirect, swap it forredirect(302).
Quick Testing Steps
- Validate your Nginx config syntax with:
nginx -t - If no errors, reload the config to apply changes:
nginx -s reload - Test accessing
https://test.com/info/byu/detail—it should now redirect tohttps://test.com/school-info?name=byu.
内容的提问来源于stack exchange,提问作者legendary_rob
相关产品推荐
相关产品推荐

