简体   繁体   中英

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?

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>

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

IIRF is free, open-source.

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 (ie, no htaccess-like mechanism)

您可以使用ISAPI_Rewrite 3或Helicon Ape产品中的上述配置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM