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

CodeIgniter部署于IIS Windows共享主机(Plesk控制面板)后URL无法去除index.php的问题求助

Fixing CodeIgniter URL Rewrite (index.php Removal) on IIS/Plesk Windows Shared Hosting

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/ (like www.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 to index.php/auth so CodeIgniter can process them—this happens behind the scenes, so users never see the index.php part.
  • defaultDocument: Makes sure IIS prioritizes index.php over 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.php is 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, and system folders (the structure you shared is correct, just verify Plesk is pointing to the right place).

4. Test the Fix

After updating all configs:

  1. Clear your browser's cache (old redirects might be cached).
  2. Visit https://www.example.com—it should load without redirecting to index.php.
  3. Visit https://www.example.com/auth—it should render correctly without index.php in the URL.
  4. 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.config is placed in the root directory (same level as index.php—not inside application or system).
  • Avoid using .htaccess on IIS—it won't work unless the host has installed an Apache compatibility layer. Stick to Web.config for Windows/IIS environments.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.01 02:07:39