设置特定域的URL重写规则

设置特定域的URL重写规则

本文介绍了设置特定域的URL重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于本地开发人员测试,我需要捕获对www.somedomain.com/XXX(其中X是路径)的所有请求,并将其发送到localhost/somevdir/XXX.

For local dev testing, I need to catch all requests to www.somedomain.com/XXX (where X is the path), and send them on to localhost/somevdir/XXX.

我已将其添加到我的HOSTS文件中(c:\ windows \ system32 \ drivers \ etc):

I've added this to my HOSTS file (c:\windows\system32\drivers\etc):

127.0.0.1   www.mydomain.com

然后,在IIS8(Windows 8)中,我为主机www.mydomain.com添加了到默认网站"的绑定.这行得通,我现在可以浏览www.mydomain.com/test.html并查看测试html页面.我的虚拟目录位于默认网站内.然后,我为网站的最后一位添加URL重写URL:

Then, in IIS8 (Windows 8), I've added a binding to my "Default Web Site" for the host www.mydomain.com. This works, I can now browse www.mydomain.com/test.html and see a test html page. My virtual dir is inside the Default web site. I then add a URL Rewrite URL to the web site for the last bit:

<rewrite>
    <rules>
        <rule name="mydomain.com" stopProcessing="true">
            <match url="^www.mydomain.com/(.*)" />
            <action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" />
        </rule>
    </rules>
</rewrite>

但是-这是行不通的.我得到404,因此看起来匹配从未发生.我尝试了重定向和重写,并且尝试了不使用正则表达式中的^和其他一些正则表达式的调整.有人可以解释我做错了什么吗?

But - that doesn't work. I get a 404 so it looks the the match never happens. I've tried redirect and rewrite, and I've tried without the ^ in the regex and a few other regex tweaks. Can someone explain what I've done wrong?

推荐答案

我认为以下方法应该有效:

I think the following should work:

<rule name="mydomain.com" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>

任何URL上的匹配均确保检查条件,并且HTTP_HOST服务器变量似乎是检查请求的主机名的最可靠方法.您可以删除REQUEST_FILENAME输入条件,但可以很好地进行检查以确保始终提供静态文件.

The match on any URL makes sure the conditions are checked, and the HTTP_HOST server variable seems the most reliable way of checking the requested hostname. You could remove the REQUEST_FILENAME input condition, but it works as quite a nice sanity check to make sure static files are always served.

这篇关于设置特定域的URL重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:59