几个星期前,我问放在这里如何检测当前页面并添加。主动类。我收到使用JavaScript以下工作的解决方案:在这里你可以查看问题:Stackoverflow问。
Few weeks ago I have asked on here how to detecting the current page and add class of .active. I have received the following working solution using JavaScript: You can view the question here: 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");
然而,我现在想从URL中的删除的扩展名为.php,我这样做,用下面的.httacess:
However, I now wanted to remove the .php extensions from the URL's and I did that using the following .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的更有效的方法?
The problem is, when I added the .httacess file above, the JavaScript stopped working... Why is that? Is there a more efficient way of doing this using PHP?
它看起来像我有固定的,但我不知道,如果这个解决方案是好的,任何建议吗? 我从index.php来/指数改变了href和它的工作原理...
It seem like I have fixed it, but I don't know if this solution is good, any advices please? I have changed the href from index.php to /index and it works...
<li><a href="/index">Home</a></li>
<li><a href="/contact">Contact</a></li>