You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

IIS7针对Public目录文件及子目录的URL重写规则需求

Hey there! Let's get your IIS URL rewrite rules set up perfectly for your PHP project. First off, make sure you have the IIS URL Rewrite module installed (you can check this in IIS Manager under "Modules" — if it's missing, grab it from the official IIS tools).

Your project lives under Root/public/, so we'll create a web.config file in the Root directory (right alongside the public folder) to handle all your requirements. Here's the complete config, followed by a breakdown of each rule:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- Redirect root domain to public/index.php -->
        <rule name="Root to Public Index" stopProcessing="true">
          <match url="^$" />
          <action type="Rewrite" url="public/index.php" />
        </rule>

        <!-- Handle root-level PHP files without .php extension (supports optional trailing slash) -->
        <rule name="Root PHP No Extension" stopProcessing="true">
          <match url="^([^/.]+)/?$" />
          <conditions>
            <add input="{DOCUMENT_ROOT}/public/{R:1}.php" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="public/{R:1}.php" />
        </rule>

        <!-- Handle subdirectory PHP files without .php extension (supports optional trailing slash) -->
        <rule name="Subdir PHP No Extension" stopProcessing="true">
          <match url="^([^/.]+)/([^/.]+)/?$" />
          <conditions>
            <add input="{DOCUMENT_ROOT}/public/{R:1}/{R:2}.php" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="public/{R:1}/{R:2}.php" />
        </rule>

        <!-- Default to index.php for subdirectories (supports optional trailing slash) -->
        <rule name="Subdir Default Index" stopProcessing="true">
          <match url="^(.+)/?$" />
          <conditions>
            <add input="{DOCUMENT_ROOT}/public/{R:1}/index.php" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="public/{R:1}/index.php" />
        </rule>

        <!-- Let static files (js, css, images) pass through without rewriting -->
        <rule name="Static Files Pass-Through" stopProcessing="true">
          <match url="^(.+\.(js|css|png|jpg|gif|ico))$" />
          <action type="None" />
        </rule>
      </rules>
    </rewrite>

    <!-- Fallback default document just in case -->
    <defaultDocument>
      <files>
        <add value="index.php" />
      </files>
    </defaultDocument>
  </system.webServer>
</configuration>

Let's break down what each rule does:

  • Root to Public Index: Catches requests to your root domain (e.g., example.com or example.com/) and rewrites them to public/index.php — exactly what you need for the home page.
  • Root PHP No Extension: Matches root-level requests like example.com/login or example.com/login/, checks if public/login.php exists, and rewrites the request to that file. This hides the .php extension cleanly.
  • Subdir PHP No Extension: Handles deeper paths like example.com/sub1/edit or example.com/sub1/edit/, verifying that public/sub1/edit.php exists before rewriting.
  • Subdir Default Index: If someone visits example.com/sub1 or example.com/sub1/, this rule checks for public/sub1/index.php and serves it as the default page for that directory.
  • Static Files Pass-Through: Ensures files like example.com/src/css/theme.css or example.com/src/js/script.js are served directly without any rewriting — no changes to their URLs.

Quick testing checklist:

  1. Visit example.com → Should load public/index.php
  2. Visit example.com/login or example.com/login/ → Should load public/login.php
  3. Visit example.com/sub1 or example.com/sub1/ → Should load public/sub1/index.php
  4. Visit example.com/sub1/edit or example.com/sub1/edit/ → Should load public/sub1/edit.php
  5. Visit example.com/src/css/theme.css → Should load the CSS file directly, no rewrite

If you run into any issues, double-check that the URL Rewrite module is enabled and that the file paths in the conditions match your actual directory structure.

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

火山引擎 最新活动