IIS重写甚至重写images / css / js - 找不到404的结果

时间:2015-11-06 15:44:20

标签: asp.net-mvc iis url-rewriting question2answer

我正在尝试重写网址以使其对搜索引擎友好

www.mydomain.com/qa/213/who-am-i

重写为

www.mydomain.com/qa/?qa=213/who-am-i

下面的块有效,但问题是页面内的js / css / images url也被重写了。因此,该页面会查找www.mydomain.com/qa/213/who-am-i/jquery.js等文件,这些文件实际上并不存在。所以页面加载,但没有css,.js和图像工作。

 <rule name="CleanRouting" stopProcessing="true">
          <match url="^qa/(.*)/(.*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/?qa={R:1}/{R:2}&amp;{QUERY_STRING}" appendQueryString="false" />
        </rule>

请告诉我如何解决这个问题。我正在使用Asp.Net MVC(如果重要的话)。

1 个答案:

答案 0 :(得分:2)

几个小时后&amp;天我终于找到了正确的IIS重写规则,用于绕过images / css / js等文件,以便页面正确显示。

必须在ASP.Net MVC项目的Web.Config中添加以下重写规则。

<!--Rewrite all paths containing a period like www.conceptworld.com/qa/453/who-am-i/qa-images/search.png to  www.conceptworld.com/qa/qa-images/search.png -->

<rule name="RewriteFileUrls" stopProcessing="true">
          <match url="^qa\/([^\/]*)\/([^\/]*)\/(.*[\.].*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/{R:2}/{R:3}" appendQueryString="false" />
        </rule>

我使用上述规则加上以下2条规则来解决类似于StackOverflow的Question2Answer问题答案基于PHP的解决方案。我在带有IIS + Asp.Net MVC的Windows系统上安装了Question2Answer

<!--Rewrites urls like www.conceptworld.com/qa/tags -->

<rule name="RewriteSingleLevelUrls" stopProcessing="true">
          <match url="^qa\/([^\/]*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/?qa={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
        </rule>

<!--Rewrites urls like www.conceptworld.com/qa/56/who-am-i -->
        <rule name="RewriteTwoLevelUrls" stopProcessing="true">
          <match url="^qa\/([^\/]*\/[^\/]*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/?qa={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
        </rule>

仅仅设置上述规则是不够的。最重要的是在Question2Answer中设置基本网址。我几乎放弃了,并认为当它安装在Windows(IIS + Asp.Net MVC)上时,在Question2Answer中不可能有漂亮的SEO友好URL,直到我发现这个设置。感谢Question2Answer开发人员:)

转到管理员/布局并设置如下所示的基本网址:

Question2Answer - Admin/Layout Page

现在,您已准备好运行Question2Answer以及IIS Asp.Net MVC。