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

反向代理后的WordPress:末尾斜杠问题与搜索引擎索引控制

Fixing WordPress Reverse Proxy Issues (IIS to Subpath)

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 WordPress wp-config.php file (located in the root of your www.myblog.com installation) 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's web.config (for the www.ourdomain.com site), 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

    1. In the root of your www.myblog.com installation, create a robots.txt file with this content:
      User-agent: *
      Disallow: /
      
      This tells all search crawlers to stay away from the internal site.
    2. 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 noindex meta tag to all pages on www.myblog.com.
  • Allow Indexing on www.ourdomain.com/blog/

    1. Since we already set WP_HOME and WP_SITEURL to the public proxy URL, WordPress will generate all links (and sitemaps) using https://www.ourdomain.com/blog/—this ensures crawlers only see the public path.
    2. 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).
    3. For extra clarity, you can add a robots.txt to the root of www.ourdomain.com with this content:
      User-agent: *
      Allow: /blog/
      Disallow: /
      
      This explicitly tells crawlers they can access the /blog/ path but not other parts of www.ourdomain.com.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 22:27:40