反向代理后的WordPress:末尾斜杠问题与搜索引擎索引控制
Hey there, let's work through these two common WordPress reverse proxy problems—they're totally solvable with the right config tweaks!
1. Trailing Slash & 404 Redirect Issue
The root cause here is that WordPress doesn't realize it's being served from a subpath (/blog/), so when it handles requests without a trailing slash, it redirects to the root domain instead of keeping the /blog/ prefix. Here's how to fix this:
Update WordPress Site URLs in
wp-config.php
Edit your WordPresswp-config.phpfile (located in the root of yourwww.myblog.cominstallation) and add these lines above the/* That's all, stop editing! Happy publishing. */comment:define('WP_HOME', 'https://www.ourdomain.com/blog'); define('WP_SITEURL', 'https://www.ourdomain.com/blog'); define('FORCE_SSL_ADMIN', true); // Tell WordPress to use the forwarded protocol/host from IIS if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { $_SERVER['HTTPS'] = 'on'; } $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'] ?? $_SERVER['HTTP_HOST'];This forces WordPress to use the public proxy URL instead of its internal domain.
Tweak IIS Reverse Proxy Rules
In your IIS server'sweb.config(for thewww.ourdomain.comsite), make sure your reverse proxy rewrite rules pass the correct headers and handle trailing slashes. Add these server variables to your rule to forward the real host/protocol to WordPress:<rewrite> <rules> <rule name="ReverseProxy to WordPress" stopProcessing="true"> <match url="^blog/(.*)" /> <action type="Rewrite" url="https://www.myblog.com/{R:1}" /> <serverVariables> <set name="HTTP_X_FORWARDED_PROTO" value="https" /> <set name="HTTP_X_FORWARDED_HOST" value="www.ourdomain.com" /> </serverVariables> </rule> <!-- Optional: Redirect requests without trailing slash to add it --> <rule name="Add Trailing Slash" stopProcessing="true"> <match url="^blog/(.+[^/])$" /> <action type="Redirect" url="/blog/{R:1}/" redirectType="Permanent" /> </rule> </rules> </rewrite>The second rule ensures all requests to
/blog/subpages get a trailing slash before hitting WordPress, preventing the wrong redirect.
2. Search Engine Indexing Control
You want to block indexing of the internal www.myblog.com while allowing the public /blog/ path to be indexed. Here's the optimal approach:
Block Indexing on
www.myblog.com- In the root of your
www.myblog.cominstallation, create arobots.txtfile with this content:
This tells all search crawlers to stay away from the internal site.User-agent: * Disallow: / - Double down by enabling the "Discourage search engines from indexing this site" option in WordPress: Go to Settings > Reading and check that box. This adds a
noindexmeta tag to all pages onwww.myblog.com.
- In the root of your
Allow Indexing on
www.ourdomain.com/blog/- Since we already set
WP_HOMEandWP_SITEURLto the public proxy URL, WordPress will generate all links (and sitemaps) usinghttps://www.ourdomain.com/blog/—this ensures crawlers only see the public path. - Make sure the "Discourage search engines..." option is unchecked in the WordPress admin (it should be by default if you set the site URLs correctly, but verify it in Settings > Reading).
- For extra clarity, you can add a
robots.txtto the root ofwww.ourdomain.comwith this content:
This explicitly tells crawlers they can access theUser-agent: * Allow: /blog/ Disallow: //blog/path but not other parts ofwww.ourdomain.com.
- Since we already set
内容的提问来源于stack exchange,提问作者bb2j3z

