【发布时间】:2016-10-11 22:36:49
【问题描述】:
我有一个 Angular 应用程序,它使用 ngRoute 模块和 HTML5 模式 pushState 来获得更清晰的 URL。
app.config(function($routeProvider, $locationProvider){
$locationProvider.html5Mode(true);
$routeProvider
//Route for home page
.when("/", {
templateUrl: "templates/main.html",
controller: "imageController",
})
//Route for about page
.when("/me", {
templateUrl: "templates/me.html",
controller: "imageController",
})
//404 error
.when("/404", {
template: "",
controller: "imageController",
title: "404 Error",
})
//Default route to /
.otherwise({
redirectTo: "/404",
});
});
我已更改我的 .htaccess 文件以重写尾部斜杠(“/”)。
RewriteEngine on
RewriteBase /
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Redirect urls without a trailing slash
RewriteRule ^(.*)/$ $1 [L,R=301]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
这对于一个尾部斜杠非常有效,例如'http://example.com/me/' 更新为 'http://example.com/me' 并得到相应的路由。
但是,当有多个斜杠时,路由会中断,例如'http://example.com/me/foo' 路由到一个不完整的页面,而不是重定向到 '/404'。 我该如何解决这个问题,以便如果“/me/foo”路由不存在,它会被重定向到“/404”。
【问题讨论】:
标签: angularjs .htaccess routing angularjs-ng-route trailing-slash