I have an Angular app using ngRoute module along with HTML5 mode pushState for cleaner 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",
});
});
I have changed my .htaccess file to rewrite trailing slashes ("/").
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]
This works great for one trailing slash, for e.g. 'http://example.com/me/' updates to 'http://example.com/me' and gets routed accordingly.
However, the routing breaks when there are multiple slashes, for e.g. 'http://example.com/me/foo' routes to an incomplete page instead of redirecting to '/404'. How do I resolve this so that if '/me/foo' route doesn't exist, it gets redirected to '/404'.