使用Html5Mode刷新页面时出现意外的令牌。

4

我在我的AngularJS应用程序中使用Html5Mode(true)。当我以以下方式浏览我的网站时,这很有效:

http://www.example.com

这将被转换为:
http://www.example.com/home/index

我的页面正常渲染。但是,当我按 F5 键时,控制台窗口中会出现很多错误信息:
Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/jquery/jquery.min.js". index:22
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://www.example.com/home/js/slider/angular.rangeSlider.css". index:16
Uncaught SyntaxError: Unexpected token < jquery.min.js:1
Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/angular/angular-resource.min.js". index:25
Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/angular/angular.min.js". index:24
Uncaught SyntaxError: Unexpected token < angular.min.js:1
Uncaught SyntaxError: Unexpected token < angular-resource.min.js:1
Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/home/js/angular/angular-cookies.min.js". index:26
Uncaught SyntaxError: Unexpected token < 

我已经按照以下方式配置了我的.htaccess文件:
RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^ - [L]

# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^(.*) /index.html [L]

但是当我刷新页面时,这个方法就失效了,我会收到错误提示。我是使用“ui-router”,但不确定是否有所不同。我的路由设置看起来像这样:
$locationProvider.html5Mode(true);

$urlRouterProvider.otherwise('/home/index');
$stateProvider
    .state('home', {
        abstract: true,
        url: '/home',
        templateUrl: '/tpl/home.html'
    })
    .state('home.index', {
        url: '/index',
        templateUrl: '/tpl/home_index.html'
    })

有人知道为什么刷新页面会导致我上面发布的错误吗?

1
我刚找到了解决方案。问题在于您正在重写所有路径到服务器。您需要创建规则来排除Content和Scripts文件夹。 - VsMaX
1个回答

0

你只需要重写需要的路由URL,例如:

/home - index.html

/product/1234 - index.html

不要将所有请求都重写到index.html,因为大多数URL不应该被重写。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接