IIS 8.0 MVC站点URL Rewrite配置导致Object Moved链接问题求助
Hey there, let's work through this issue step by step—your problem makes total sense once we break down what's happening.
Why You're Seeing "Object Moved to here"
When you set up your URL Rewrite rule to replace the entire RESPONSE_Location variable with an empty string, you're breaking every redirect that your MVC site or IIS tries to send.
Browsers expect a valid URL in the Location header for 3xx redirect responses. If that header is empty, the browser can't figure out where to send the user, so it falls back to showing that generic "Object Moved" message with a broken empty link. Your goal was to hide internal IPs, not kill all redirects entirely!
Fixes to Try
Let's adjust your approach to target only the internal IP part of the Location header, not the whole thing.
1. Use a Regex Outbound Rule to Replace Only the Internal IP
Instead of a wildcard replace, create a regex-based rule that finds your internal IP in the Location header and swaps it out for your public domain/path. Here's how to set it up:
- Open IIS Manager, go to your site, and launch URL Rewrite.
- Create a new Outbound Rule with these settings:
- Match Conditions:
- Condition input:
{RESPONSE_Location} - Match type:
Regular Expression - Pattern:
http(s)?://192\.168\.1\.100/(.*)(replace192.168.1.100with your actual internal IP; escape dots with backslashes) - Check "Ignore case"
- Condition input:
- Action:
- Action type:
Rewrite - Replace value:
http$1://your-public-domain.com/$2(swapyour-public-domain.comwith your site's actual public URL) - Leave "Replace" checked, and skip "Stop processing subsequent rules" unless you have conflicting rules.
- Action type:
- Match Conditions:
This way, you only strip out the internal IP while preserving the rest of the redirect path—so your links and redirects will work normally, and the internal IP won't be exposed.
2. Check for Hardcoded Internal IPs in Your MVC Code
Sometimes the Location header gets populated with internal IPs because your MVC controllers are using hardcoded internal URLs in Redirect() or RedirectToAction() calls. Do a quick search in your codebase for your internal IP address—if you find any instances, replace them with your public domain or use relative paths instead. This fixes the issue at the source, which is more reliable than relying on rewrite rules.
3. (Only if you have NO redirects) Remove the Location Header Entirely
If your site truly has no need for redirects (unlikely for most MVC sites), you can delete the Location header instead of setting it to empty. To do this:
- In IIS Manager, go to your site's HTTP Response Headers feature.
- Click "Add" > select "Remove" from the dropdown, enter
Locationas the header name, and save.
But again, only use this if you're 100% sure you don't need any redirects—otherwise, stick to the regex rule above.
Verify the Fix
After setting up the rule, open your browser's dev tools (F12 > Network tab), load your site, and check the response headers for any redirects. You should see the Location header uses your public domain instead of the internal IP, and your links/redirects should work without the "Object Moved" message.
内容的提问来源于stack exchange,提问作者Chiefster

