Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 : Allow trailing slash in routes

I'm updating an old website with Angular, and one of the requirements I have to meet is that all routes should remain the same as they were (For SEO purposes).

Many of the old website's routes finish with a slash (Like /my/route/), and some of them finish with a .html, like /my/route.html.

The issue is that routerLink deletes the final slash in every route finishing by a slash (My route is now /my/route).

How can I make routerLink to keep the trailing slash ?

A light example can be seen here : AngularTrailingSlash.

like image 285
Marc Brillault Avatar asked Jan 24 '18 14:01

Marc Brillault


People also ask

How to preserve the trailing slash in a page URL Angular 2+?

In this article, we will learn routing in Angular 2+ along with the process of preserving the trailing slash in a page URL with the addition of current page content, which means, we will retain the data of any page while doing a page refresh. In simple terms, while refreshing the page, we want to remain on the same page with the same page data.

What is a trailing slash in PHP?

Trailing slash means a backslash at the end of the URL. Now, the question is what is the need for that. Let me explain this with an example. Suppose, we have a page URL like above http://localhost:4200/about.

How do I set up routes in the angular CLI?

Import RouterModule and Routes into your routing module. The Angular CLI performs this step automatically. The CLI also sets up a Routes array for your routes and configures the imports and exports arrays for @ NgModule (). Define your routes in your Routes array. Each route in this array is a JavaScript object that contains two properties.

How to add components for routing link in angular?

Adding components for routing link. To use the Angular router, an app needs to have at least two components so that it can navigate from one to the other. To create a component using the CLI, enter the following at the command line where first is the name of your component: content_copy. ng generate component first.


1 Answers

I added below to your app.module.ts

import {Location, PathLocationStrategy} from '@angular/common';


const _orig_prepareExternalUrl = PathLocationStrategy.prototype.prepareExternalUrl;

PathLocationStrategy.prototype.prepareExternalUrl = function(internal) {
  const url = _orig_prepareExternalUrl.call(this, internal);

  if (url === '') {
    return url;
  }

  console.log('For ' + internal + ' we generated ' + url);
  if (url.endsWith('.html')) {
      return url;
  }

  if (url.endsWith('/')) {
    return url;
  }


  return url + '/';

};

Location.stripTrailingSlash = function (url) {
    const /** @type {?} */ match = url.match(/#|\?|$/);
    const /** @type {?} */ pathEndIdx = match && match.index || url.length;
    const /** @type {?} */ droppedSlashIdx = pathEndIdx - (url[pathEndIdx - 1] === '/' ? 1 : 0);
    const first = url.slice(0, droppedSlashIdx);
    const last = url.slice(pathEndIdx);

    if (first.endsWith('.html')) {
        return first + last;
    }

    return first + '/' + last;

};

And it now works for me with a trailing slash when .html is not there

like image 75
Tarun Lalwani Avatar answered Oct 30 '22 03:10

Tarun Lalwani