【发布时间】:2018-07-25 10:18:21
【问题描述】:
几周前,我在这里问过如何检测当前页面并添加 .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");
然而,我现在想从 URL 中删除 .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 来做到这一点?
【问题讨论】:
标签: javascript php .htaccess