skip to Main Content

I’ve changed my angular app from using hash (#) to non-hash and everything works perfectly.

Some of my users have bookmarked the site with the hash, and therefore I would like to redirect all hash routes to a non-hash route.

I can do it in the code, but I prefer to do it before, because of SEO considerations. Maybe at the level of Heroku?

2

Answers


  1. Try this

    export class AppComponent implements OnInit {
        constructor (private router: Router) { }
    
        ngOnInit() {
          this.router.events.subscribe(event => {
            if (event instanceof NavigationStart) {
              if (!!event.url && event.url.match(/^/#/)) {
                this.router.navigate([event.url.replace('/#', '')]);
              }
            }
          });
        }
    }
    

    Let me know in case of any doubt.

    Login or Signup to reply.
  2. export class AppComponent {
        constructor (private router: Router) { 
         this.router.events.subscribe(event => {
            if (event instanceof NavigationStart) {
              if (!!event.url && event.url.match(/^/#/)) {
                this.router.navigateByUrl(event.url.replace('/#', ''));
              }
            }
          });
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search