DEV Community

Anderies
Anderies

Posted on • Updated on

Remove hash(#) at URL Angular and Still be able to refresh and Not Getting 404 Not Found !

recently i’m building a movie stream app and decide to use angular.

if you have 404 not found error when refreshing or you have ugly hash url on your web and projects
this article is really for you this article suited you if you use Angular 4 | 5 | 6 |7 |8 | 9 and older version

Problem :

After i’m finished coding and deploying my angular app at shared hosting Server and found out if i am refreshing one of my movie detail component it Got error 404 Not Found,
And than i really trying to solve this for a while by adding this :

To my project app-routing.module.ts

scrollPositionRestoration: ‘top’, useHash: true,

and this to my project app.module.ts

providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],

And the results is my movie detail component can be refreshed and not getting 404 Not Found anymore but the url looks ugly ( for me ) it shows
www.example.com/#/movie-detail

Solutions:

This is problem had me struggle about 2 days can’t be solved by me , i try to googling and search on youtube old video tutorial on angular JS but that’s not working.
And in the moment i got this article really helpfull content contains about angular lastest version routing , more deep explanation and trying to implement it on my projects

(https://gist.github.com/julianpoemp/bcf277cb56d2420cc53ec630a04a3566#new-generator)

The solution is simple you just need to make .htaccess (make sure it’s not txt format) on your Public_html that contains this :
.htaccess

and changes this on your angular projects

And boom your Angular project gets regular url like www.example.com/movie-detail without hash and able to Refresh and not getting 404 Not Found anymore

I know this is not a problem for senior developer but it’s kinda BIG AND ANNOYING PROBLEM for junior developer or engineer that first time using angular and i just want to them to have the lastest article , because sometimes developer more interest and believe lastest article.

i hope this article can be grow angular developer numbers around the world

if you can’t solve Angular Project problem just Send me messages
i’d love to help and answer it :) ✌️

just follow my github here maybe i’ll update more free projects 🎮 soon
(https://github.com/Anderies)

Top comments (3)

Collapse
 
rajdeepshakya profile image
Rajdeep-shakya

I am using AWS windows server

Collapse
 
rajdeepshakya profile image
Rajdeep-shakya

This code is not working for me please help me

Collapse
 
wilsenhc profile image
Wilsen Hernández

You can also note that this fix only applies when you're using an Apache server