简体   繁体   English

网址重写在IIS上有效,但在localhost中不可用

[英]Url rewriting works on IIS but doesn't in localhost

I use url rewriting for my web site. 我为我的网站使用URL重写。 I did settings on IIS and it works on server. 我在IIS上进行了设置,并且可以在服务器上使用。 But it doesn't work on localhost. 但是它在localhost上不起作用。 It is normal because there is no page with rewrited url in my project files. 这是正常现象,因为在我的项目文件中没有页面带有重写的url。 How can I solve this problem? 我怎么解决这个问题? I use cassini server when I develop my project. 开发项目时,我使用卡西尼服务器。 Should I use local IIS in my computer? 我应该在计算机上使用本地IIS吗? You can see here my url rewriting roles in web.config file: 您可以在此处在web.config文件中看到我的url重写角色:

 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <outboundRules>
                <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)ProductDetail\.aspx\?prid=([^=&amp;]+)&amp;(?:amp;)?product=([^=&amp;]+)$" />
                    <action type="Rewrite" value="{R:1}ProductDetail/{R:2}/{R:3}/" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
            <rules>
                <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                    <match url="^urun/([^/]+)/([^/]+)/?$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="ProductDetail.aspx?prid={R:1}&amp;product={R:2}" />
                </rule>

            </rules>
        </rewrite>
        <urlCompression doDynamicCompression="false" />
  </system.webServer>

Why don't use Url Routing instead? 为什么不使用网址路由呢? it's better way 这是更好的方法

Yes, you have to install IIS on your local computer using Add/Remove Windows Components. 是的,您必须使用“添加/删除Windows组件”在本地计算机上安装IIS。

Make sure also to enable the "URL Rewrite module" within your local IIS, once it's installed. 安装后,还请确保在本地IIS中启用“ URL重写模块”。

You need to add a negate condition <add input="{HTTP_HOST}" pattern="localhost" negate="true" /> so the URL rewriter ignores any requests on localhost. 您需要添加否定条件<add input="{HTTP_HOST}" pattern="localhost" negate="true" />以便URL重写程序忽略对localhost的任何请求。

<rewrite>
  <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
    </rule>
  </rules>
</rewrite>

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

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