在ionic這個框架下(Angular JS),對URL進行重寫,過濾掉URL中的#號

Eric Sun發表於2015-01-26

此時URL的改變已經完全不受後臺程式碼控制了,因此我們要在前端的ionic這個框架和IIS中進行修改調控。

其實IIS只是host了整個站點,具體的URL跳轉都是由前端來控制的。

1):那麼前端要加上一行程式碼:

$locationProvider.html5Mode(true);

 

2):IIS對應的web.config要做如下處理,是每次經過IIS的請求都跳回主頁,那麼後續的URL管理都由前臺框架來管理了:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Main Rule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

 

3):要保證上述config能夠生效,需要安裝IIS URL Rewrite的外掛,具體下載地址如下所示:

http://www.iis.net/downloads/microsoft/url-rewrite 

 

更多詳細的資訊請看如下連結:

https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

 

相關文章