10

Within angular routing is there any way to redirect to an external directory on the same domain. I have a WordPress blog and angular app I want it to use same domain rather than a subdomain.

Example

Please help me with your suggestions.

Edric
  • 24,639
  • 13
  • 81
  • 91
Kesara Wimal
  • 639
  • 13
  • 22

5 Answers5

7

I understand why you wanna do this with Angular router. Navigating to an external url from an Angular application is something quite easy. Using window.location or an anchor tag is straight forward, but it has a big disadvantage, it bypasses the Angular Router.

@Adrian proposed a nice and generic way of navigating to an external url using the Angular Router which is written inside Using the Angular Router to navigate to external links. I like his implementation which looks clean and correct. He prepared the final result in stackblitz too.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
  • I haven't had a chance to actually try this impl, but I feel like this is the most "correct" way to implement it because it doesn't bypass the Router. Actually, I hadn't considered the route guards at all, but now that I have, there's no doubt that I want to continue using the router. :) – VolatileRig Jul 20 '20 at 16:02
2

If understood correctly, it should work with redirection based on content type.

  • Solution:

Redirects (301, 302, 303, 307) to internal/external page or file, based on domain and path:


Other option is to use DNS redirects at redirect.center as an example.


Last and final, to use WordPress plugin which at first look seems to have many options to try out.


Sall
  • 31
  • 7
1

You can simply use window.open(url, '_blank'); it will open new tab with url that set in to it

CodeMind
  • 616
  • 1
  • 7
  • 19
1

The angular router is meant to provide the routes within the angular app and has no inherent external routing.

You could route to an almost empty component that redirects to your external link. That seems to be the simplest solution.

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { BlogComponent } from './blog/blog.component';    

const routes: Routes = [
  { path: 'blog', component: BlogComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

blog.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  template: ''
})
export class BlogComponent implements OnInit {
  ngOnInit(): void {
    window.location.href = 'http://example.com/blog';
  }
}

PS: Ensure you can access your wordpress installation without being redirected to the angular app by your server.

Rob Monhemius
  • 4,822
  • 2
  • 17
  • 49
  • I think question is asked for angularjs1.x. – pavinan Jul 18 '20 at 04:28
  • @pavinan It's been tagged as both. Which seems kinda weird, since they are mutually exclusive tags. That said I am not familiar with `angularjs`. So maybe someone who does can use this answer as inspiration or point out any similarities. – Rob Monhemius Jul 18 '20 at 04:40
1

I think you can use redirect to function like the following code sample.

$routeProvider.when("/blog", {
  redirectTo: function () {
    window.location.href = window.location.origin + "/blog";
    // remove below line if it is not working.
    return "/some redirecting animation page";
  }
});

if using state provider.

var aboutState = {
  name: 'blog',
  url: '/blog',
  redirectTo: function () {
    window.location.href = window.location.origin + "/blog";
    // remove below line if it is not working.
    return "/some redirecting animation page";
  }
}
pavinan
  • 1,275
  • 1
  • 12
  • 23