如何将此.htaccess转换为IIS web.config?

时间:2014-08-26 11:42:54

标签: .htaccess iis backbone.js history web-config

将此行转换为IIS web.config的正确方法是什么?我正在尝试使用支持历史记录的Backbone路由器。它与.htaccess完美配合,但我不知道如何在IIS上执行此操作。

.htaccess文件

# html5 pushstate (history) support:
<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteRule (.*) index.html [L]
</ifModule>

这是index.html

中包含的脚本
$(document).ready(function(){
    var AppRouter = Backbone.Router.extend({
        routes: {
            '': 'home',
            'about(/)': 'about', //about or /about/ will call the same function
            'work(/:id)' : 'work',
            "*path"  : "notFound"
        }
    });
    // Initiate the router
    var app_router = new AppRouter;

    app_router.on('route:home', function() {
        $("#log").html("home");
    });
    app_router.on('route:about', function() {
        $("#log").html("about");
    });
    app_router.on('route:work', function(id) {
        $("#log").html("work " + id);
    });
    app_router.on('route:notFound', function(path) {
        $("#log").html("404 " + path);
    });

    Backbone.history.start({pushState: true, root: "/post43/"});

    $(document).on("click", "a:not([data-bypass])", function(e) {
        // Get the anchor href and protcol
        var href = $(this).attr("href");
        // Stop the default event to ensure the link will not cause a page refresh.
        e.preventDefault();
        Backbone.history.navigate(href, true);
    });
});

1 个答案:

答案 0 :(得分:2)

经过一番挖掘,这段代码可以在IIS上运行。在转换我的htaccess时,我正在寻找this guide。我希望它能帮助那些正在寻找这种解决方案的人。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="history" stopProcessing="true">
                    <match url="(.*)" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{URL}" pattern="^index$" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.html" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
        <defaultDocument>
            <files>
                <remove value="index.html" />
                <add value="index.html" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>