【问题标题】:JavaScript & .htaccess rules are clashingJavaScript 和 .htaccess 规则发生冲突
【发布时间】: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


    【解决方案1】:

    看来我已经解决了,但我不知道这个解决方案是否好,请问有什么建议吗? 我已将 href 从 index.php 更改为 /index 并且可以正常工作...

    <li><a href="/index">Home</a></li>
    <li><a href="/contact">Contact</a></li>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 2011-07-27
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多