简体   繁体   English

URL重写不适用于IIS 7

[英]URL Rewriting not working on IIS 7

I have a PHP site deployed on IIS 7 and using URL Rewrite module but my rewrite rules are not working. 我在IIS 7上部署了一个PHP站点并使用URL重写模块,但我的重写规则不起作用。 Below are my actual url and urls I want to show in browser: 以下是我想在浏览器中显示的实际网址和网址:

Browser URL : http://mydomain.com/myfolder or http://mydomain.com/myfolder/anytext 浏览器URL: http//mydomain.com/myfolderhttp://mydomain.com/myfolder/anytext

Actual URL : http://mydomain.com/myfolder/myfile.html 实际网址: http//mydomain.com/myfolder/myfile.html

Previously I was using mod rewrite with .htaccess on Wamp server and below are the working rules which were defined in .htaccess file 以前我在Wamp服务器上使用mod重写和.htaccess,下面是.htaccess文件中定义的工作规则

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} ^(.+)/$

RewriteRule ^(.+)/$  /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^.*$ myfile.html [L]

Below is my web.config file which is not working, Please suggest and help to resolve my problem 下面是我的web.config文件无效,请建议并帮助解决我的问题

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="Rewrite to myfile.html1">
                <match url="^(.+)/$" />
                <action type="Rewrite" url="/$1" />
            </rule>
        </rules>
        <rules>
            <rule name="Rewrite to myfile.html2">
                <match url="^.*$" />
                <action type="Rewrite" url="myfile.html" />
            </rule>
        </rules>
    </rewrite>
    </system.webServer>
</configuration>

The .htaccess rules are actually doing two different things. .htaccess规则实际上做了两件事。 First of all it makes sure that requests ending with a / (slash) are redirected to the URL without and ending slash. 首先,它确保以/ (斜杠)结尾的请求被重定向到URL而没有结束斜杠。 And the second rule rewrites all request for non-existing files to myfile.html . 第二条规则将对所有不存在文件的请求重写为myfile.html

This should work (untested): 这应该工作(未经测试):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="Removing trailing slash" stopProcessing="true">
                <match url="^(.+)/$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Redirect" url="/{R:1}" />
            </rule>
        </rules>
        <rules>
            <rule name="Rewrite to myfile.html" stopProcessing="true">
                <match url="^.*$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Rewrite" url="/myfile.html" />
            </rule>
        </rules>
    </rewrite>
    </system.webServer>
</configuration>

After some hit and tries this web.config worked for me 经过一番点击并尝试这个web.config为我工作

<?xml version="1.0" encoding="UTF-8"?>
   <configuration>
      <system.webServer>
         <directoryBrowse enabled="true" />
         <rewrite>
            <rules>
               <rule name="Rule1" stopProcessing="true">
                  <match url="^(.+)/$" />
                  <conditions>
                     <add input="{URI}" pattern="^(.+)/$" />
                     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                  <action type="Rewrite" url="/$1" />
               </rule>
               <rule name="Rule2" stopProcessing="true">
                  <match url="^myfolder/.*$" />
                  <conditions>
                     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  </conditions>
                 <action type="Rewrite" url="myfolder/myfile.html" />
              </rule>
           </rules>
        </rewrite>
     </system.webServer>
  </configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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