You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
routing doesnt work after deployment in angular2 #2885
angular-cli: 1.0.0-beta.18
node: 6.4.0
os: win32 x64 Windows 10
I have created an angular2 app & deployed it successfully.
app starts correctly after taking some time to load & then I am able to navigate to different components.
but when I put direct address in URL as :
www.sample.com/about
it says 404 error.
even when I am creating bundle using ng build --prod , it creates dist folder correctly & I am able to navigate to different components using links only.
but I cant access components directly from the URL in my local system also.
I am using http-server.
are there any extra configurations required to make it work?
Yes - you do :) Angular CLI does not automatically add routing to the app just yet therefore you would need to modify app.module.ts file as example below:
Reacted by Moeid Saleem khan, MohamedAmin700, Dmytro Verbovyi, Sudharsan Tettu, Théo Constant, KV, Laxman Pai, elfentech, GO-MADsolutions, Victoria and 21 more
http-server doesn't support fallback to index.html, thus you can't use HTML5 push navigation with it.
You can use https://github.com/johnpapa/lite-server instead, which supports fallback.
Reacted by Mythical Programmer, Gus, Saravana Saran R, souravMitra92, Prof. Libório Júnior, maheshkumawat24 and AasthaWadhwa01
Hi , I have the same issue , its working on ng serve . But if I use ng build I cant hit directly i get "Error cannot GET/path" . I was not able to implement the the hash location strategy . can somebody help me with this
Now you can build the solution for production using ng build --prod
This will create a dist folder. The files inside dist folder are ready for deployment by any mode.
Reacted by stickelt, tazmanrising, Dinh, gökhan öztürk, Srashti Jain, harish mahajan and LordNashor
Reacted by piyarulpss, Domenick DiBiase, siripuramrj and devajith
I have the same issue... "ng serve" works great and changes the url according to route configuration. "ng build --prod" changes the url according to route configuration but they cannot be navigated again
that worked for me! I assume this fallback thing is supported in an amazon server as well, right? (we're planning to upload the front end project there)
You need to configure this on the server, direct access e.g. www.yourdomain.eu/home will look for "home" directory which does not exist... All requests need to go through index.html so on apache server all you need is .htaccess from this https://angular.io/guide/deployment
find this line , it should be in routes file
RouterModule.forRoot(appRoutes);
and change it to
RouterModule.forRoot(appRoutes, {useHash: true});
2nd solution : keep this as your last route, if you dont like hash based urls
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
Apache: add a rewrite rule to the .htaccess file with following content
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
Apache: add a rewrite rule to the .htaccess file with following content
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
Edit that file and write index.html instead of index.php
For those who dont have any code, can write the code below in your .htaccess file :
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
Adding .htaccess file to my root directory worked for me, also check httpd.conf file to ensure rewrite_module is being loaded (by default it's commented.)
angular-automatic-lock-bot commented on Sep 9, 2019
Activity
deebloo commentedon Oct 25, 2016
you need to setup something to redirect to your index.html page or change the default location strategy to hash.
hbweb commentedon Oct 25, 2016
Hi,
Yes - you do :) Angular CLI does not automatically add routing to the app just yet therefore you would need to modify
app.module.tsfile as example below:Bhushan001 commentedon Oct 25, 2016
@hbweb HI I tried this,still its not redirecting to components if I am redirecting directly from URL :(
clydin commentedon Oct 25, 2016
As @deebloo mentioned, you need to properly configure your server or switch to angular's hash location strategy.
34r7h commentedon Oct 27, 2016
+1 Then pushing to gh-pages is probably not much good since we can't update their servers for proper html5mode. Same as with ng1.x, no?
deebloo commentedon Nov 1, 2016
@irthos you can change the router to use A hash location strategy instead of the history API and that will work on gh pages.
filipesilva commentedon Nov 3, 2016
http-serverdoesn't support fallback to index.html, thus you can't use HTML5 push navigation with it.You can use https://github.com/johnpapa/lite-server instead, which supports fallback.
naveenkumarkg19 commentedon Jun 22, 2017
Hi , I have the same issue , its working on ng serve . But if I use ng build I cant hit directly i get "Error cannot GET/path" . I was not able to implement the the hash location strategy . can somebody help me with this
filipesilva commentedon Jun 28, 2017
@naveenkumarkg19 see https://angular.io/guide/deployment for information about deployment. Likely you need to configure index fallback (https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml).
MalateshPatil commentedon Nov 6, 2017
If this issue occurred in production, follow below steps
ng build --prodThis will create a dist folder. The files inside dist folder are ready for deployment by any mode.
lsalim commentedon Nov 10, 2017
Hi. Did anybody tested the last solution above? Is it working?
piyarulpss commentedon Nov 17, 2017
Nothing Works.... :-(
chezihp commentedon Nov 28, 2017
I have the same issue... "ng serve" works great and changes the url according to route configuration. "ng build --prod" changes the url according to route configuration but they cannot be navigated again
gustavostuff commentedon Nov 30, 2017
@filipesilva "http-server doesn't support fallback to index.html, thus you can't use HTML5 push navigation with it. You can use https://github.com/johnpapa/lite-server instead, which supports fallback."
that worked for me! I assume this fallback thing is supported in an amazon server as well, right? (we're planning to upload the front end project there)
1 remaining item
34r7h commentedon Dec 1, 2017
@deebloo Would we still be able to go directly to deep rooted pages and get SEO with that approach?
amocarski commentedon Jan 22, 2018
@Bhushan001 U have to add something like that in ur
server.jsapp.get('/*', function(req, res) {res.sendFile(path.join(__dirname + '/dist/index.html'));});toshikverma commentedon Feb 4, 2018
@amocarski refreshing on any page will lead to index.html only
chacabuk commentedon Mar 14, 2018
Create a Web.Config file on the same folder just like MalateshPatil says.
And nothing more!
Works
tomandquery commentedon Apr 25, 2018
You need to configure this on the server, direct access e.g. www.yourdomain.eu/home will look for "home" directory which does not exist... All requests need to go through index.html so on apache server all you need is .htaccess from this https://angular.io/guide/deployment
Gvo87 commentedon Jun 7, 2018
Had the same issue, fixed like this
I was using <a href="https://github.com/settings"
changed to <a (click)="navTo('/settings')"
this worked for me
But if you are setting it on aws S3 you need to configur s# and cloudfront
https://medium.com/@willmorgan/moving-a-static-website-to-aws-s3-cloudfront-with-https-1fdd95563106
sunling commentedon Jul 13, 2018
Working example: https://github.com/sunling/random-quote
Heroku link:https://randomquote621.herokuapp.com/
hiteshSharma6 commentedon Aug 18, 2018
Angular Deployment guide: https://angular.io/guide/deployment
codebanesr commentedon Aug 31, 2018
find this line , it should be in routes file
RouterModule.forRoot(appRoutes);
and change it to
RouterModule.forRoot(appRoutes, {useHash: true});
2nd solution : keep this as your last route, if you dont like hash based urls
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
sks40gb commentedon Sep 19, 2018
Apache: add a rewrite rule to the .htaccess file with following content
qqwrv-f10eq commentedon Sep 25, 2018
This work for me !!
smitpatelx commentedon Oct 3, 2018
For Apache Server
.htaccessindex.htmlinstead ofindex.php.htaccessfile :ali4j commentedon May 27, 2019
Adding .htaccess file to my root directory worked for me, also check httpd.conf file to ensure rewrite_module is being loaded (by default it's commented.)
angular-automatic-lock-bot commentedon Sep 9, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.