My html page url is www.example.com/training/product.html. I want to change my url like to www.example.com/training/product. Is it possible using javascript or Jquery? How?
Asked
Active
Viewed 1.8k times
4
-
2Seems like something you should be handling server side. – cookie monster Aug 20 '14 at 05:38
-
1you can try changing `window.location.href`, but after changing it might not work, this should be on server side. – Mritunjay Aug 20 '14 at 05:38
-
http://stackoverflow.com/questions/6478485/jquery-change-the-url-address-without-redirecting – rnrneverdies Aug 20 '14 at 05:40
4 Answers
7
Sorry I can not comment due to Reputation-Restriction.
The right solution is not yet in the comments.
window.history.replaceState()
does the job.
I would do:
var link = 'www.example.com/training/product.html';
link.split('.html')[0];
window.history.replaceState( null, null, link );
For a more respected solution go to How to reliably get the filename without the suffix in javascript?
Link:

Community
- 1
- 1

user32342534
- 145
- 6
-
3No need to make this a comment, this is the correct answer (with a documentation link even), plus now you're only 1 pt away from the comment priv. For browser compatibility (IE mainly has issues with this solution) there's also a solution which uses the hash (`#`) at the end of the URL to manipulate the text. – nbrooks Aug 20 '14 at 06:05
-
1
Using javascript
you can achieve this like:
var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
if (a.indexOf('html') > -1) { //Check of html String in URL.
url = url.substring(0, newURL.lastIndexOf("."));
}
If you are looking at Server level changes, Below are the rules for .htaccess file
RewriteEngine On
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .html requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://example.com/folder/$1 [R=301,L]
# Resolve .html file for extensionless html urls
RewriteRule ^([^/.]+)$ $1.html[L]

Vivek Pratap Singh
- 9,326
- 5
- 21
- 34
0
Try this using Jquery :-
var url = "www.example.com/training/product.html";
url = url.substring(0, url.lastIndexOf("."));

Kartikeya Khosla
- 18,743
- 8
- 43
- 69