Reputation: 1977
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.
Upvotes: 8
Views: 12676
Reputation: 650
Below code worked for me:
export const contactUsRoutes: Routes = [
{
path: 'contact-us/.', component: ContactUsComponent
}
];
. .
<a [routerLink]="['/contact-us/.']">Contact us</a>
And add below code in your main.ts:
import { Location } from '@angular/common';
const __stripTrailingSlash = (Location as any).stripTrailingSlash;
(Location as any).stripTrailingSlash = function _stripTrailingSlash(url: string): string {
const queryString$ = url.match(/([^?]*)?(.*)/);
if (queryString$[2].length > 0) {
return /[^\/]\/$/.test(queryString$[1]) ? queryString$[1] + '.' + queryString$[2] : __stripTrailingSlash(url);
}
return /[^\/]\/$/.test(url) ? url + '.' : __stripTrailingSlash(url);
};
I have referred below link for this solution:
https://stackblitz.com/github/ssatz/Angular5-Preserve-Trailing-Slash
Upvotes: 2
Reputation: 2821
Even more improved code (based on code from Umesh).
Add on top of main.ts
:
import { Location } from "@angular/common";
// Prevent stripping of trailing slash
Location.stripTrailingSlash = function stripTrailingSlashNew(url: string) {
return url;
};
Upvotes: 2
Reputation: 146520
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
Upvotes: 6
Reputation: 6106
Try with Location class extends in AppModule
like this:
app.module.ts:
import { Injectable } from '@angular/core';
import { Location } from '@angular/common';
@Injectable()
export class UnstripTrailingSlashLocation extends Location {
public static stripTrailingSlash(url: string): string {
return url;
}
}
Location.stripTrailingSlash = UnstripTrailingSlashLocation.stripTrailingSlash;
@NgModule({
...
Upvotes: 0
Reputation: 121
Add this code in the main.ts file to keep trailing slash in the routerLink.
import { Location } from '@angular/common';
const __stripTrailingSlash = (Location as any).stripTrailingSlash;
(Location as any).stripTrailingSlash = function _stripTrailingSlash(url: string): string {
return /[^\/]\/$/.test(url) ? url : __stripTrailingSlash(url);
}
// import main ng module
// bootstrap on document ready
Upvotes: 0