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.comorexample.com/) and rewrites them topublic/index.php— exactly what you need for the home page. - Root PHP No Extension: Matches root-level requests like
example.com/loginorexample.com/login/, checks ifpublic/login.phpexists, and rewrites the request to that file. This hides the.phpextension cleanly. - Subdir PHP No Extension: Handles deeper paths like
example.com/sub1/editorexample.com/sub1/edit/, verifying thatpublic/sub1/edit.phpexists before rewriting. - Subdir Default Index: If someone visits
example.com/sub1orexample.com/sub1/, this rule checks forpublic/sub1/index.phpand serves it as the default page for that directory. - Static Files Pass-Through: Ensures files like
example.com/src/css/theme.cssorexample.com/src/js/script.jsare served directly without any rewriting — no changes to their URLs.
Quick testing checklist:
- Visit
example.com→ Should loadpublic/index.php - Visit
example.com/loginorexample.com/login/→ Should loadpublic/login.php - Visit
example.com/sub1orexample.com/sub1/→ Should loadpublic/sub1/index.php - Visit
example.com/sub1/editorexample.com/sub1/edit/→ Should loadpublic/sub1/edit.php - 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




