Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling routes without hashbang using angular-ui-router

I've searched a lot online for an answer to this but haven't found anything yet.

Right now I have this code in my file that I thought would handle all routes/states besides the ones I've specified:

$urlRouterProvider.otherwise('/');

However, this functionality only works for paths like localhost:1337/#/stuff, but if I type in localhost:1337/stuff, I get an ugly internal server error.

Ideally, I'd like localhost:1337/stuff or any other URL without a hashbang to redirect to localhost:1337/#/ (my app's homepage).

Any thoughts on how I can accomplish this?

like image 717
syu15 Avatar asked Jun 28 '15 00:06

syu15


People also ask

Is it possible to handle AngularJS 1x routing with angular-route (ngroute)?

I started with Angular 2+, but my current company has some projects that require AngularJS 1x. So, here we are! I this post, I will show you how to handle AngularJS 1x routing by using Angular-Route (ngRoute). There will be another post that I will use UI-Router for handling AngularJS routing.

What is the use of Ui router in angular?

AngularJS Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.

How to include Route parameters in angular router?

To handle such scenario angular router allows us to include route parameters, where we can send any dynamic value for a URL segment We can define parameter by adding forward slash followed colon and a placeholder (id) as shown below Now above path matches the URLs /product/1 , /product/2, etc.

How do I create a routing-app using the angular CLI?

The following command uses the Angular CLI to generate a basic Angular app with an app routing module, called AppRoutingModule, which is an NgModule where you can configure your routes. The app name in the following example is routing-app. When generating a new app, the CLI prompts you to select CSS or a CSS preprocessor.


1 Answers

You need to set up two things:

  1. A so-called "html5Mode" in your AngularJS configuration. That's the easy part: $locationProvider.html5Mode(true);
  2. Set rewrites on your server so any request renders only your main html page.

From AngularJS docs on routing:

Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html).

like image 142
Tomek Sułkowski Avatar answered Oct 15 '22 11:10

Tomek Sułkowski