几周前我在这里问过如何检测当前页面并添加.active类。我使用JavaScript收到了以下工作解决方案:您可以在此处查看问题:Stackoverflow Question。
$("#mainMenu a").filter(function () {
var _href = location.href.replace(/#.*/, "");
if (location.pathname === "/") {
// change http://example.com/ to http://example.com/index.php
_href += "index.php";
}
return _href === this.href;
}).addClass("active");
然而,我现在想从网址中删除.php扩展名,我使用以下内容删除了.httacess:
DirectoryIndex index.html index.php
ErrorDocument 404 /404
ErrorDocument 504 /504
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301,L]
# remove index
# By puting the L-flag here, the request gets redirected immediately
# The trailing slash is removed in a next request, so be efficient and
# dont put it on there at all
RewriteRule (.*)/index$ $1 [R=301,L]
# remove slash if not directory
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]
# add .php to access file, but don't redirect
# On some hosts RewriteCond %{REQUEST_FILENAME}.php -f will be true, even if
# no such file exists. Be safe and add an extra condition
# There is no point in escaping a dot in a string
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(/|\.php)$
RewriteRule (.*) $1.php [L]
问题是,当我在上面添加.httacess文件时,JavaScript停止工作......为什么会这样? 使用PHP有更有效的方法吗?
答案 0 :(得分:0)
好像我已经修好了,但我不知道这个解决方案是否合适,有什么建议吗? 我已经将index.php中的href更改为/ index并且它可以工作......
<li><a href="/index">Home</a></li>
<li><a href="/contact">Contact</a></li>