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

IIS 8 URL Rewrite规则配置:忽略URL末尾目录但保留显示

IIS 8 URL Rewrite Rule to Ignore (But Keep) Trailing Arbitrary Directory

Got it, let's solve this problem for you. You want URLs like https://mywebsite.com/stores/somestore to render the /stores page correctly (instead of throwing a 404), while keeping the full URL intact so your PHP app can use that trailing somestore value—just like how Facebook handles profile URLs. Here's exactly how to set this up with IIS URL Rewrite:

Prerequisite

First, make sure you have the IIS URL Rewrite Module installed on your server (it's a free official IIS extension; you can grab it via IIS Manager's "Get New Web Platform Components" if you don't have it already).

The Rewrite Rule (web.config)

Add this rule to your site's web.config file in the root directory (or edit it via IIS Manager's URL Rewrite interface):

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Ignore Trailing Store Segment" stopProcessing="true">
          <!-- Match URLs starting with /stores/ followed by any non-empty string -->
          <match url="^stores/(.+)$" />
          <!-- Skip the rule if the request is for a real file or directory -->
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <!-- Internally rewrite the request to /stores, but keep the original URL visible -->
          <action type="Rewrite" url="stores" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

How It Works

Let's break down each part:

  • Match Pattern: ^stores/(.+)$ targets any URL under /stores/ that has at least one character after the slash (so it catches /stores/somestore, /stores/123, etc.).
  • Conditions: The two negated checks ensure we don't interfere with real files or directories (like /stores/style.css or /stores/admin—those will still load normally).
  • Rewrite Action: Instead of redirecting (which would change the URL), we use an internal rewrite to point the server to /stores. The browser will still show the full original URL, so your PHP app can access that trailing segment.

Accessing the Trailing Segment in PHP

Since we can't use URL parameters, you'll need to parse the original request URI in your PHP code. Here's a simple way to get that trailing store name:

// Get the full request URI (e.g., "/stores/somestore")
$requestUri = trim($_SERVER['REQUEST_URI'], '/');
// Split into parts
$uriParts = explode('/', $requestUri);

// Check if the first segment is "stores" and there's a second segment
if (count($uriParts) >= 2 && $uriParts[0] === 'stores') {
    $storeName = $uriParts[1];
    // Use $storeName to render your page (e.g., fetch store data, etc.)
}

Quick Tips

  • Make sure /stores/index.php (or your default document) is set as an IIS default document, so the rewritten request loads your entry point.
  • If you have other rewrite rules, place this one near the top of the list (since stopProcessing="true" will halt further rule processing once this matches).
  • Test with incognito mode to avoid browser caching old 404 responses.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:18:28