Deploy angular application on Apache Tomcat 404 deep links issue

后端 未结 2 1702
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 15:31

I\'m trying to figure out the way how to setup Apache Tomcat server to serve angular application with deep links. For example:

A static server routinely returns ind

2条回答
  •  -上瘾入骨i
    2020-12-05 16:04

    I managed to fix this problem e.g http://localhost:8080/angular/player/detail/4 with following steps:

    1) Build angular application with: ng build --prod -bh ./ -d /angular

    2) Copy contents of builded app to $ApacheLocation/webapps/angular

    3) Rewrite rule:

    RewriteCond %{REQUEST_PATH} !-f
    RewriteRule ^/angular/(.*) /angular?path=$1
    

    4) Setup navigation at app.component.ts:

    constructor(private activatedRoute: ActivatedRoute, private router: Router) { }
    
    ngOnInit() {
    const path = this.activatedRoute.snapshot.queryParams['path'];
    const navigateTo = '/' + path;
    
    if (path) {
      this.router.navigate([navigateTo]);
    }
    

    }

    You can find test project here: https://github.com/avuletica/test

    Explanation of rewrite rules:

    # %{REQUEST_PATH} corresponds to the full path that is used for mapping.
    # ! NOT character 
    # '-f' (is regular file) Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
    # ^ matches the beginning of the string or line
    # . matches any charceter except line breaks
    # * match 0 or more of the preceding token
    

    So basically if there is no file on /angular/anything tomcat sends full path as query string to angular application and from that query param angular handles routing.

提交回复
热议问题