Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, URL rewriting with htaccess, and Microsoft IIS Url Rewriting

I am used to working with Apache servers, so when mod_rewrite is enabled, I can create an htaccess file and use URL rewriting.

Here's my htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Now I've built this site that uses this URL rewriting module but I have come to learn that it is a Microsoft server. Can I use my htaccess file? Is there something I need to change to get it to work? How can I tell if URL rewriting is set up on the Microsoft server?

like image 824
Andrew Avatar asked Jun 22 '09 21:06

Andrew


3 Answers

If you use IIS 7 then you can use IIS URL Rewrite Module, that has an "Import Rules" feature that can be used to translate mod_rewrite rules to IIS URL rewrite format. These particular rewrite rules will not translate because the RewriteCond uses the "-s" and "-l" flags which check if the requested URL corresponds to a non-zero size file or to a symbolic link on a file system. If your application does not use any symbolic links then you can safely replace these conditions with:

RewriteCond %{REQUEST_FILENAME} -f [OR]

and then convert the rules by using IIS URL Rewrite UI. That will result in these rules:

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^.*$" />
      <conditions logicalGrouping="MatchAny">
        <add input="{REQUEST_FILENAME}" matchType="IsFile"  />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory"  />
      </conditions>
      <action type="None" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
      <match url="^.*$" />
      <action type="Rewrite" url="index.php" />
    </rule>
  </rules>
</rewrite>
like image 89
RuslanY Avatar answered Nov 12 '22 12:11

RuslanY


You can use your configuration as is, in Ionic's Isapi Rewrite Filter (IIRF).

IIRF is free, open-source.

like image 23
Cheeso Avatar answered Nov 12 '22 11:11

Cheeso


The .htaccess file is an Apache convention for providing end-user access to Apache configuration, so you're not going to be able to use it as a drop in replacement on an IIS (Microsoft) server. You would be able to use it if you were running Apache on Windows.

IIS7 has a URL rewriting module that offers support for rewriting URLs. There's also the ISAPI_Rewrite product which does the same for previous versions of IIS. You'll likely need some level of administrative permissions on the server to use either of these modules (i.e., no htaccess-like mechanism)

like image 29
Alan Storm Avatar answered Nov 12 '22 10:11

Alan Storm