将Gallery3 .htaccess mod_rewrite代码转换为IIS URL在Web.config中重写

时间:2012-01-21 11:05:13

标签: asp.net .htaccess mod-rewrite url-rewriting web-config

我有一个网站,我已经安装了Gallery3。网址是

 http://techblog.lalsofttech.com/gallery/

但是当我打开“测试专辑”时,在url中有index.php

http://techblog.lalsofttech.com/gallery/index.php/test

现在我想要的是从网址中删除index.php并希望网址看起来像这样

 http://techblog.lalsofttech.com/gallery/test

由于我的共享服务器空间是带有IIS 7的Windows平台,因此我无法使用.htaccess文件。 由于我的服务器安装了Microsoft URL Rewrite模块,我需要在web.config文件中编写重写规则。

这是将index.php隐藏在.htaccess文件中的代码

 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /gallery
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L]
  RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L]
  RewriteRule ^index.php/(.*) $1 [QSA,R,L]
 </IfModule>

我尝试通过在localhost中安装Microsoft URL Rewrite模块来转换它。

除了“RewriteBase / gallery”之外,所有其他代码都被转换。“RewriteBase / gallery未转换,因为IIS不支持”是我收到的错误消息。 这是转换后的代码。

<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
  <match url="^(.*)$" ignoreCase="false" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
  </conditions>
  <action type="Rewrite" url="index.php?kohana_uri={R:1}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
  <match url="^$" ignoreCase="false" />
  <action type="Rewrite" url="index.php?kohana_uri={R:1}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
  <match url="^index.php/(.*)" ignoreCase="false" />
  <action type="Redirect" redirectType="Found" url="{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>

但似乎这段代码不起作用,因为index.php仍然存在。 ISS URL重写模块正在运行,因为我在web.config中添加的另一个规则“Enforce canonical hostname”正常工作。

这是我完整的web.config文件。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <directoryBrowse enabled="false" />
  <rewrite>
  <rules>
    <rule name="Enforce canonical hostname" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.moviega\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.moviega.com/{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="index.php?kohana_uri={R:1}" appendQueryString="true" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
      <match url="^$" ignoreCase="false" />
      <action type="Rewrite" url="index.php?kohana_uri={R:1}" appendQueryString="true" />
    </rule>
    <rule name="Imported Rule 3" stopProcessing="true">
      <match url="^index.php/(.*)" ignoreCase="false" />
      <action type="Redirect" redirectType="Found" url="{R:1}" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>
</system.webServer>
</configuration>

问题出在哪里?应该怎么做才能从url中删除index.php? 请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

这可能与您放置此Web.config文件的位置 1 有关。只要它在http://www.MovieGa.com/gallery/子目录中,我认为它应该有用......!

修改 在回答您的第二个问题时,这可能是错误发生的原因:

这与这部分有关:

<rule name="Imported Rule 2" stopProcessing="true">
  <match url="^$" ignoreCase="false" />
  <action type="Rewrite" url="index.php?kohana_uri={R:1}" appendQueryString="true" />
</rule>

使用 .htaccess,我觉得这很好用。但是使用 Web.config ,它会给你这个错误,因为 2 它试图在正则表达式中找到捕获组,而在这个特定的<match url="^$">部分中没有。如果您从?kohana_uri={R:1}删除url=""部分,它是否有效?

参考:

1 http://forums.iis.net/t/1162232.aspx(Anil Ruia的评论)

2 http://forums.iis.net/t/1150904.aspx(ruslany的第二条评论)

答案 1 :(得分:0)

好的,我会在这里说清楚,对于那些在带有IIS7的Windows Server中安装图库3的人 并试图从网址中删除index.php文件。

第一步:将您的web.config文件与重写规则一起保存在您安装图库的目录中,而不是您网站的根目录

第二:你放在gallery文件夹中的web.config文件的重写规则应该是这样的

<?xml version="1.0" ?>
<configuration>

<system.webServer>

<rewrite>
 <rules>
   <rule name="Imported Rule 1" stopProcessing="true">
     <match url="^(.*)$" ignoreCase="false" />
     <conditions>
       <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
     </conditions>
     <action type="Rewrite" url="index.php?kohana_uri={R:1}" appendQueryString="true" />
   </rule>
   <rule name="Imported Rule 2" stopProcessing="true">
     <match url="^()$" ignoreCase="false" />
     <action type="Rewrite" url="index.php?kohana_uri={R:1}" appendQueryString="true" />
   </rule>
   <rule name="Imported Rule 3" stopProcessing="true">
     <match url="^index.php/(.*)" ignoreCase="false" />
     <action type="Redirect" redirectType="Found" url="{R:1}" appendQueryString="true" />
   </rule>
 </rules>
</rewrite>

</system.webServer>
</configuration>

希望这对那些遇到同样问题的人有用..