can not route directly using browser url what can i do? - angularjs

I cannot route directly by using input in browser.
I am using $locationProvider.html5Mode(true);
so when i write the url in browser then they cannot route in angular js.
AppRouting.js
myApp.config(['$routeProvider',
function($routeProvider) {
$locationProvider.html5Mode(true);
$routeProvider.
when('/', {
templateUrl: 'home.html',
controller: 'HomeController'
})
.when('/login', {
templateUrl: 'login.html'
})
.otherwise({
redirectTo: '/phones'
});
}]);
so what can i do???

Your web server must be able to redirect your URL for it to work.
Say you are on "http://site" and you have a link leading to "http://site/foo/bar".
When you click on this link, you don't really go on this URL but rather on "http://site/index.html#/foo/bar", angularjs handles the rest.
However, if you directly access "http://site/foo/bar", your web server will try to find a path "/foo/bar" on your server, which does not exist, so you have to configure your server to redirect everything to "index.html".
As you're using Apache, you can use to add this in your .htaccess:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
More info here.

Related

After Removeing # From angularjs with Codeigniter website URL ,then if reload then its give 404 page not found...(main Error for page not reload)? [duplicate]

This question already has answers here:
Removing the fragment identifier from AngularJS urls (# symbol)
(14 answers)
Closed 4 years ago.
After Removeing # From angularjs with Codeigniter website URL ,then html is not load properly in main ng-view.(main Error for page not reload)
What should i do for overcome this problem.
var app = angular.module('main-App', ['ngRoute','angularUtils.directives.dirPagination']);
app.config(['$routeProvider','$locationProvider',
function ($routeProvider,$locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'templates/home.html',
controller: 'AdminController'
}).
when('/items', {
templateUrl: 'templates/items.html',
controller: 'ItemController'
}).
when('/users', {
templateUrl: 'templates/user_view.html',
controller: 'ItemController'
}).
when('/wearhouse',{
templateUrl:'templates/wearhouse_view.html',
controller:'ItemController'
}).
when('/units',{
templateUrl:'templates/units_view.html',
controller:'ItemController'
}).
when('/product',{
templateUrl:'templates/product_view.html',
controller:'ItemController'
}).
when('/productListing',{
templateUrl:'templates/product_listing_view.html',
controller:'ItemController'
}).
when('/stock',{
templateUrl:'templates/stoke_view.html',
controller:'ItemController'
}).
otherwise(
{redirectTo : '/'}
);
$locationProvider.html5Mode(true);
}]);
.htaccess file in codeigniter:-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Its is happening because when you remove hash from your url, your server tries to map the entire url and then provide the data to client.
When you have # in your url, the server ignores whatever comes after # and simply maps the path which is there before #
For: www.something.com\resoucePath\#abc\def
server simply ignores abc\def and this is how angularJS exploits routing.
Where as for : www.something.com\resoucePath\abc\def
server goes ahead and find url mapping or #path matching \abc and then \def
You need to configure your server to handle such requests. I has the same issue once, this link will surely help you get some idea

Url refresh and reload not working after removing # sign from url angularjs

My app.js
app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'interests.html',
controller: 'InterestsController'
})
.when('/interests',{
templateUrl: 'interests.html',
controller: 'InterestsController'
})
.when('/subjects',{
templateUrl: 'subjects.html',
controller: 'SubjectsController'
})
.when('/prequalification',{
templateUrl: 'prequalification.html',
controller: 'PreQualificationController'
})
.when('/qualification',{
templateUrl: 'qualification.html',
controller: 'QualificationController'
})
.when('/personalinfo',{
templateUrl: 'personalinfo.html',
controller: 'PersonalInfoController'
})
.otherwise({
redirectTo : '/interests'
});
if(window.history && window.history.pushState){
$locationProvider.html5Mode(true);
}
}]);
In index.html file. Path to index.html file: localhost:8080/cf/. All of the other views also resides with index.html
<base href="/cf/">
.htaccess file content(path to .htaccess file: localhost:8080/cf/.htaccess)
<ifModule mod_rewrite.c>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ cf/index.html [L]
</ifModule>
Path to my project folder on server is: localhost:8080/cf. I have various rules in .htaccess file from other posts of stackoverflow but nothing is working. When even i reload or refresh the page, i get 404. With out refreshing and reloading everything is working fine. Please help!!!
Step 1: Add this in app config
if(window.history && window.history.pushState){
$locationProvider.html5Mode(true);
}
Step 2: Add this in index.html file
<base href="/your-app-name">
Step 3: Add .htaccess file on root directory
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
Use
<base href="/cf">
in document "head" section.
Hope this helps!!

BASE tag with locationProvider AngularJS

My English is not very good, but i try to explain my self clear. I'm trying to optimize Website based on pure Angular to SEO.
Here is the link http://www.sanjosejumpstart.com/tests/project/
When you come first time to the website and start clicking on the links, routing is working fine, but when refreshing the page, i get error with
Not Found
The requested URL /tests/project/automotive was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
i can't understand why this happens.(may be because a hash is disappeared?)
Some body have any idea how i can fix that?
BASE tag on index html looks
<base href="http://www.sanjosejumpstart.com/tests/project/">
and route config look likes:
.config(['$httpProvider', '$stateProvider', '$urlRouterProvider', '$locationProvider',
function($httpProvider, $stateProvider, $urlRouterProvider, $locationProvider) {
$httpProvider.defaults.transformRequest.push(LockJS.postConvert);
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.interceptors.push('Interseptor');
/* activating html5 mode */
$locationProvider.hashPrefix('!');
$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/home',
views: {
'': {
templateUrl: 'templates/main-page-content.html'
},
'article#home': {
templateUrl: 'templates/main-page/app-main-article.html',
controller: 'MainArticleCtrl'
},
'services#home': {
templateUrl: 'templates/main-page/app-main-services-list.html',
controller: 'MainServicesCtrl'
},
'services-info#home': {
templateUrl: 'templates/main-page/app-services-info.html',
controller: 'MainServicesInfoCtrl'
},
'aside#home': {
templateUrl: 'templates/main-page/app-aside-content.html'
},
'middle-slider#home': {
templateUrl: 'templates/main-page/app-middle-slider.html'
},
'map#home': {
templateUrl: 'templates/main-page/app-map.html'
}
}
})
.state('automotive', {
url: '/automotive',
views: {
'': {
template: '<h1>automotive view</h1>'
}
}
})
}])
And then comes more states.
Thank you for any advice.
You need to set up your server to return your index.html page for all requests that are not:
an API call (have these all start with /api to make this easier)
a static file request (have these all start with /pub or similar)
Then the index.html will load, Angular will bootstrap your app, which will involve examining the route and loading the correct page.
Why should I use /api or /pub?
The exact prefix is not super important (though it should make sense). What is helpful is that by using a common prefix for all API endpoints (e.g. routes you call using the $http or $resource services), you will not overlap with any client-side routes in your app.
For example, if you didn't follow this rule, say you had the following server side route configured:
/products - gets a list of products as JSON
Then you had a client-side route set up as: /products, it becomes a bit confusing as to which one you meant when you type in www.myserver.com/products.
Technically, if the server is configured correctly it could tell the difference between an AJAX request to /products asking for JSON and a "normal" HTTP GET request to /products. But it is still not ideal, because you can't tell from the URL alone which one you meant. You have to look at how it was called as well.
Alternative: Give up on HTML5 mode
If you can't configure the server like I said above, you will need to disable HTML5Mode. That way, all the relative URLs will be after the #, so the only file request the web server ever gets is for the default page for /test/project.
i found the solution, you can see all redirects now:
http://www.sanjosejumpstart.com/tests/project/home
here is the solution:
Apache server
1) index must be php, not html - index.php
2) adding BASE tag in head of the file like this = <base href="/tests/project/">
3) in app config - $locationProvider.html5Mode(true)
no need to add $locationProvider.hashPrefix('!');
and finally .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Locksmith-Project/project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php/#/$1
</IfModule>
Hope it is be helpful to someone

AngularJS routing issue with apache

I am using Yeoman package manager to create an angular web application. this is my main controller:
'use strict';
angular
.module('resFourApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
])
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl'
})
.when('/contact', {
templateUrl: 'views/contact.html',
controller: 'ContactCtrl'
})
.otherwise({ redirectTo: '/' });
// use the HTML5 History API
// use the HTML5 History API
$locationProvider.html5Mode(true);
});
which works fine if I access the app from the base directory but if I access it from the url and go to www.base.com/route I get a 404 error. Which I understand is because Angular is a client side app so there is nothing on the server to handle these requests. I have read a lot online about having to reroute with server or mod rewrites with apache, but I still have not gotten it working.
Things I have tried
Apache rewrite rules not being applied for angularjs
https://groups.google.com/forum/#!msg/angular/GmNiNVyvonk/mmffPbIcnRoJ
my .htaccess for apache web server:
# BEGIN angularJS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.html/#/$1
</IfModule>
# END angularJS
I've tried a few things with rewriting and couldn't get it to work but found a workable solution using redirection and the NE flag
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ #/$1 [NC,R,NE]
There seems to be quite a few other interesting links from this SO answer regarding the hash/ URL fragment: https://stackoverflow.com/a/15135130

angularJS how to user accessible url routing?

The basic routing method like below,
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers']).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/view1'});
$locationProvider.html5Mode(true);
}]);
I want make user can access view1 by typing url "://host/view1" on browser. but this is not work..
By using $routeProvider and $locationProvider, if user click "href=/view1" this link, then
page routed host/view1.
But If user access directly to the url "://host/view1" it emit 404 error.
To make user can access /view1 directly by url, I have to remove html5Mode then, user can access by "://host/#/view1"
but How can I remove the # symbol!? not only just html5mode, the method user can access the page directly by typing that url.
Thanks. Yoshi
I create .htaccess file on DocumentRoot folder and add below
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
into .htaccess file.
that works as I expected!
ps.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
above two lines means if the file with the specified name in the browser doesn't exist, or the directory in the browser doesn't exist then procede to the rewrite rule below

Resources