简体   繁体   English

IIS重写不起作用(但重定向确实)

[英]IIS Rewrite not working (but redirection does)

I was trying to play with URL re-writing using the Rewrite Module 2.0 but I had no luck getting it to work. 我正在尝试使用Rewrite Module 2.0进行URL 重写,但我没有运气让它工作。 What I'm trying to do is re-write all calls to web app at port 80 to other applications hosted in IIS (or maybe on different servers on the network). 我要做的是将端口80上的所有对Web应用程序的调用重新写入IIS中托管的其他应用程序(或者可能在网络上的不同服务器上)。 Using the GUI provided by IIS I created the following rule: 使用IIS提供的GUI我创建了以下规则:

<rewrite>
    <rules>
        <rule name="ReverseProxyInboundRule1" stopProcessing="true">
            <match url="site1/(.*)" />
            <action type="Rewrite" url="http://localhost:7001/{R:1}" />
        </rule>
    </rules>
</rewrite>

Quiet simple, but unfortunately it does not work. 安静简单,但不幸的是它不起作用。 On the other hand, when I change the action type to Redirect , it works fine. 另一方面,当我将操作类型更改为Redirect ,它可以正常工作。

What could be the problem? 可能是什么问题呢?

I ran into this same issue yesterday, and it took me a long time to figure out. 昨天我遇到了同样的问题,我花了很长时间才弄明白。

The key here is that you've got an http:// prefix in your rewrite action; 这里的关键是你的重写动作中有一个http://前缀; that makes this a special case that needs to be handled by Application Request Routing. 这使得这是一个需要由应用程序请求路由处理的特殊情况。 The first step is to make sure that the Application Request Routing module is installed. 第一步是确保安装了“应用程序请求路由”模块。 You can find the module at https://www.iis.net/downloads/microsoft/application-request-routing . 您可以在https://www.iis.net/downloads/microsoft/application-request-routing找到该模块。 Once that is installed, go to your IIS web server (a level up from your web site), and open the Application Request Routing Cache feature. 安装完成后,转到IIS Web服务器(从您的网站升级),然后打开“应用程序请求路由缓存”功能。 From the actions on the right, choose Server.Proxy.Settings , and make sure that the "Enable Proxy" checkbox is checked. 从右侧的操作中,选择Server.Proxy.Settings ,并确保选中“启用代理”复选框。 This allows the URL rewrite task to be re-routed to Application Request Routing, and your reverse proxy should work for external requests. 这允许将URL重写任务重新路由到应用程序请求路由,并且您的反向代理应该适用于外部请求。

The idea came from this excellent blog post from 2009: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/ 这个想法来自2009年的优秀博客文章: http//ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/

Stumbled across this old post when I was trying to solve the same issue. 当我试图解决同样的问题时偶然发现了这篇旧帖子。

SOLVED! 解决了!

Using Rewrite URL feature in IIS Services Manager I created a friendly URL rule. 在IIS服务管理器中使用重写URL功能,我创建了一个友好的URL规则。

This worked ok and when I looked at the rule in the web.config file (www root) it showed 1 rule to redirect and 1 rule to rewrite. 这工作正常,当我查看web.config文件(www root)中的规则时,它显示了1个重定向规则和1个重写规则。

I edited this to suit 1 match. 我编辑了这个以适应1场比赛。 Then I just duplicated this code editing the product ID for each. 然后我只是复制了这段代码,编辑了每个代码的产品ID。 Example below: 示例如下:

            <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                <match url="^product\.php$" />
                <conditions>
                    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                    <add input="{QUERY_STRING}" pattern="^id_product=\b35\b" />
                </conditions>
                <action type="Redirect" url="990mm-bohemia-cast-iron-electric-radiator" appendQueryString="false" />
            </rule>

The first rule looks for the string "product.php" in the URL and "id_product=35" , it then redirects to "990mm-bohemia-cast-iron-electric-radiator" which currently does not exist. 第一条规则在URL中查找字符串"product.php"并且"id_product=35" ,然后重定向到目前不存在的"990mm-bohemia-cast-iron-electric-radiator" Then (see below) 然后(见下文)

            <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                <match url="^\b990mm-bohemia-cast-iron-electric-radiator\b" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="product.php?id_product=35" />
            </rule>

This rule rewrites the "product.php?id_product=35" bit to `990mm-bohemia-cast-iron-electric-radiator", creating the new location for the redirect. 此规则将"product.php?id_product=35"位重写为“990mm-bohemia-cast-iron-electric-radiator”,为重定向创建新位置。

I had 404 error with url rewrite. url重写我遇到404错误。 Maybe you can find it usfull. 也许你可以找到它usfull。 Please check it at https://stackoverflow.com/a/47988082/5784543 请访问https://stackoverflow.com/a/47988082/5784543查看

Do make sure MVC routing doesn't steal your request. 确保MVC路由不会窃取您的请求。 To prevent that from happening, ignore the route you're trying to rewrite: 要防止这种情况发生,请忽略您尝试重写的路径:

RouteTable.Routes.Ignore("blog/{*pathInfo}");

Inspired by: https://sitecore.stackexchange.com/questions/3645/how-to-setup-a-reverse-proxy-with-sitecore 灵感来自: https//sitecore.stackexchange.com/questions/3645/how-to-setup-a-reverse-proxy-with-sitecore

Change the Rewrite URL to AbsolutePath instead putting http://... it should be 将重写URL更改为AbsolutePath,而不是将http:// ...更改为

It worked for me, but in my case, I have been rewrite to a fixed webpage. 它对我有用,但就我而言,我已经改写为固定的网页。

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

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