Could someone please help me convert the following .htaccess file to web.config?

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]

I tried to use Rewrite Module to translate it but it fails for the following:

  1. RewriteBase / This is not supported in IIS

  2. RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L] IIS also fails to translate this.


In web.config rewrite and remove index.php will work like this:

        <rule name="Rule" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                <add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
            <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />

Visit this URL for more information.

  • Thank you. This <add input="{HTTP_HOST}" pattern="^example\.com$" /> and <action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />. Is there a way to make it read the URL from current visiting website instead of manually editing the link? – Gon Feb 9 '17 at 10:49
  • Nope, But you can ignore that part with little change. I have edited my answer. please try this updated code (Not tested by me [let me know if it not work out.]) – Mufi - Amici Infotech Feb 9 '17 at 11:49
  • It does not remove index.php automatically but when I type in something like http://localhost/contact instead of http://localhost/index.php/contact it still works. – Gon Feb 9 '17 at 23:19
  • Yes. htaccess is works like this. You run site without index.php not remove index.php.. that's a trick. – Mufi - Amici Infotech Feb 10 '17 at 4:02
  • Sry I tried to edit the comment but it would not let me. What I meant was when we type in something like this http://localhost/index.php/contact a link with index.php. It should rewrite it to none index.php link but all traffic will need to direct through index.php if that makes sense. – Gon Feb 10 '17 at 5:26

I found this one on Microsoft website. It works. Mufi's answer run URLs without index.php and with index.php too. This code below rewrite and remove index.php from URL.

      <rule name="Redirect index.php" stopProcessing="true">
        <match url="index\.php/(.*)" />
        <action type="Redirect" url="{R:1}" appendQueryString="false" />
      <rule name="Rewrite index.php">
        <match url="(.*)" />
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <action type="Rewrite" url="index.php/{R:1}" />

Visit the following link for more info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.