如何删除或更改.html扩展在我的url使用javascript或jQuery
How to remove or change .html extension in my url using javascript or jQuery?
我的html页面url是www.example.com/training/product.html.我想改变我的url喜欢www.example.com/training/product.是否可能使用javascript或Jquery?如何?
抱歉,由于声誉限制,我不能评论。
正确的解决方案还没有在评论中。
window.history.replaceState()
起作用
我会这样做:
var link = 'www.example.com/training/product.html';
link.split('.html')[0];
window.history.replaceState( null, null, link );
对于一个更受尊重的解决方案去如何可靠地获得文件名没有javascript后缀?
<标题>链接:https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history The_replaceState () .C2.A0method
标题>您可以通过使用MVC来做到这一点。这是Javascript无法做到的。这需要在服务器端完成。
使用javascript
,您可以这样实现:
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("."));
}
如果您正在查看服务器级别的更改,下面是.htaccess文件
的规则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]
尝试使用Jquery:-
var url = "www.example.com/training/product.html";
url = url.substring(0, url.lastIndexOf("."));
相关文章:
- 清单文件中的update_url禁用了我的chrome扩展
- 为什么不'我的扩展程序不会出现在Chrome网上商店的搜索结果中
- 用jQuery扩展我的类
- 谷歌扩展:如何在弹出窗口中按下按钮时在我的javascript文件中运行函数
- 浏览器扩展程序来跟踪我的谷歌活动
- 为什么我的谷歌浏览器扩展程序在谷歌浏览器版本 41.0.2272.89 中不起作用
- 尝试从我的 chrome 扩展程序访问 gmail 中的 iframe 时出现未定义的错误,但不是从开发者控制台访问
- 要将 Chrome 扩展程序更改为 manifest_version 2,需要执行哪些操作?我的扩展程序只是在弹出窗口中
- 如何缩小我的谷歌chrome扩展's的javascript文件
- 如何在不接触类代码的情况下扩展我预先存在的javascript类
- 为什么我的firefox扩展javascript不能访问opener窗口,也看不到window.name
- 为什么我的 init() 函数没有运行?(安装 Firefox 扩展时自动添加工具栏按钮,但仅在首次运行时)
- 我的镀铬扩展程序没有't向控制台显示错误,但它显示日志
- chrome扩展///我的代码运行了不止一次
- 扩展我的JavaScript代码
- 不能用Prototype扩展我的javascript对象
- 如何在页面加载扩展我的Div
- 如何在脸书上扩展我的视频.js播放器
- 为什么灯箱扩展我的网页
- 我如何让pixi扩展我的画布并放大