Add end trailing slash to URL using jquery: Hi Friends, Today I will tell you about one seo url issue in which trailing slash(/) is necessary in the end of url. Actually search engine’s think that slash and non-slash url are different but it is same from our side. To remove the confusion of search engine we are forcing trailing end slash with the help of url rewriting, htaccess or javascript.
You have to add slash if slash is not there in the end of url and my code is also won’t work when any query string(?) is present in the url because if you add slash in the end of the query string url, you page is not opening correctly.
Trailing slash issue is the issue of slash in which search engine thinks you have two different page of same url and user thinks that if slash is not there at the end of URL, that means page is loading. You could always force a end trailing slash.
For adding the end slash you may write some jquery code. These code and it’s explanation are below
Open you code in any code editor.
Paste the code in any master js file which is accessible to all pages.
$(document).ready( function() { fnAddEndSlashUrl(); }); String.prototype.insertAt=function(index, string) { return this.substr(0, index) + string + this.substr(index); } function fnAddEndSlashUrl() { var url = window.location.href; var urlLength = url.length; if(url.indexOf('?') == -1) { if(url.substr(urlLength - 1) != '/') { url = url.insertAt(urlLength,'/'); window.location.href = url; } } } |
$(document).ready( function() { fnAddEndSlashUrl(); }); String.prototype.insertAt=function(index, string) { return this.substr(0, index) + string + this.substr(index); } function fnAddEndSlashUrl() { var url = window.location.href; var urlLength = url.length; if(url.indexOf('?') == -1) { if(url.substr(urlLength - 1) != '/') { url = url.insertAt(urlLength,'/'); window.location.href = url; } } }
In this code I have made two functions. These function works are
Don’t forget to add jquery library, otherwise the above code is not worked.
Read About Other Interesting Things about jquery below.
I hope you will like Add End Trailing Slash To URL Using Jquery. If You found it useful please subscribe my blog.
Categories: Search Engine Optimization
Leave a Reply