CodeIgniter部署于IIS Windows共享主机(Plesk控制面板)后URL无法去除index.php的问题求助
Let's work through this issue where your CodeIgniter app on Plesk-managed IIS Windows hosting isn't removing index.php from URLs correctly, and root domain access redirects to the messy index.php version. Here's how to resolve it step by step:
1. First, Update CodeIgniter's Core Config
Start with the basics in your CodeIgniter config file—this is often the missing piece:
Open application/config/config.php and verify these settings:
// Set your full base URL (include https:// and trailing slash) $config['base_url'] = 'https://www.example.com/'; // Remove index.php from the URL by setting this to an empty string $config['index_page'] = ''; // Use REQUEST_URI for reliable URI parsing on IIS $config['uri_protocol'] = 'REQUEST_URI';
2. Fix the Web.config Rewrite Rules
Your existing Web.config is missing a rule to redirect existing index.php URLs to clean ones, and the rewrite logic can be adjusted for IIS behavior. Replace your current Web.config with this:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <!-- Rule 1: Redirect any index.php/... URLs to clean URLs (permanent redirect) --> <rule name="Remove index.php from URL" stopProcessing="true"> <match url="^index\.php/(.*)$" ignoreCase="false" /> <action type="Redirect" url="{R:1}" redirectType="Permanent" /> </rule> <!-- Rule 2: Rewrite clean URLs back to index.php (internal rewrite, no visible change) --> <rule name="Rewrite to index.php" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{R:1}" pattern="^(resources|robots\.txt)" ignoreCase="false" negate="true" /> </conditions> <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" /> </rule> </rules> </rewrite> <!-- Ensure IIS loads index.php first when accessing the root domain --> <defaultDocument> <files> <clear /> <add value="index.php" /> <add value="default.aspx" /> <add value="default.htm" /> </files> </defaultDocument> </system.webServer> </configuration>
What this does:
- Rule 1: Catches any URL with
index.php/(likewww.example.com/index.php/auth) and redirects it to the clean version (www.example.com/auth) with a permanent 301 redirect (good for SEO). - Rule 2: Takes clean URLs (like
www.example.com/auth) and internally rewrites them toindex.php/authso CodeIgniter can process them—this happens behind the scenes, so users never see theindex.phppart. - defaultDocument: Makes sure IIS prioritizes
index.phpover other default files when you visit the root domain, preventing unexpected redirects.
3. Verify Plesk Hosting Settings
Since this is a shared Plesk host, double-check these settings:
- Enable URL Rewriting: Log into Plesk, go to your domain's Web Hosting Access (or Settings > Apache & nginx Settings depending on your Plesk version), and ensure the URL Rewriting module is enabled. Some shared hosts disable this by default.
- Default Documents: In Plesk, navigate to Settings > Default Documents for your domain. Confirm
index.phpis at the top of the list—if not, move it there. - Confirm Root Directory: Make sure your website's physical root directory points to the folder containing your
index.php,application, andsystemfolders (the structure you shared is correct, just verify Plesk is pointing to the right place).
4. Test the Fix
After updating all configs:
- Clear your browser's cache (old redirects might be cached).
- Visit
https://www.example.com—it should load without redirecting toindex.php. - Visit
https://www.example.com/auth—it should render correctly withoutindex.phpin the URL. - Test manually entering
https://www.example.com/index.php/auth—it should automatically redirect to the clean URL.
Common Troubleshooting Tips
- If the rewrite rules don't work, contact your host to confirm the IIS URL Rewrite module is installed on the server (some shared hosts skip this).
- Ensure your
Web.configis placed in the root directory (same level asindex.php—not insideapplicationorsystem). - Avoid using
.htaccesson IIS—it won't work unless the host has installed an Apache compatibility layer. Stick toWeb.configfor Windows/IIS environments.
内容的提问来源于stack exchange,提问作者user16554178

