【问题标题】:Replacing hash with slash in address bar with Javascript用Javascript在地址栏中用斜线替换哈希
【发布时间】:2014-03-06 17:26:01
【问题描述】:

我有如下的 ajax 链接:

www.domain.com/profile#dashboard

是否可以通过 htaccess 将其更改为:

www.domain.com/profile/settings

如果是这样,怎么做? :D

编辑:

这是我的 loadPages .js 文件:

(document).ready(function() {

if (location.href.indexOf("#") > -1)
    location.assign(location.href.replace('#', "/"));

    //preload pages
    if(window.location.hash) {
        //Loading
        var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character      
        loadContent( hash+".php" );     }   
});


function loadContent(pagina) {
    jQuery("#page").load(pagina);
}

这是我的 .htaccess 文件

RewriteEngine On
RewriteBase /

#Hide de submap pages
RewriteRule ^$ pages/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ pages/$1

#Hide de logged-in file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ logged-in.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/logged-in\.php [NC]
RewriteRule ^ %1 [R=301,L]

这就是我链接页面以在#pages div - 仪表板页面中打开的方式

<a class="iconNav" href="#dashboard" id="loadDashboard" onclick="loadContent('dashboard.php');">

我更新了我的 .htaccess 文件。现在链接看起来像:

www.domain.com/#dashboard

我希望它是:

www.domain.com/dashboard

【问题讨论】:

  • # 之后的 URL 部分甚至没有到达 Apache,它必须在 Javascript 本身中完成。
  • 我刚刚更新了我的问题

标签: javascript .htaccess hash slash


【解决方案1】:

您可以使用location.assign ( url )方法在浏览器中更改URL:

if (location.href.indexOf("#") > -1)
    location.assign(location.href.replace('#', "/"));

【讨论】:

  • 看到 500 错误时,您在浏览器中看到的 URL 是什么?您能否将 Apache error.log 也用于 500 错误的实际原因。
  • 您没有提供足够的信息来更好地调查它。我问是哪个 URL 导致 500 错误。
  • 这 500 与这个 Javascript 无关。甚至http://mijn.dannyhtek.nl/ 也会导致 500 错误。
  • 我已经编辑了 .htaccess 文件,它不再返回 500 错误。它加载没有任何样式的dashboard.php页面,即使它应该有样式。
  • 但是你没有发布任何.htaccess,这就是为什么我无法调查这方面的任何事情。
猜你喜欢
  • 2011-03-12
  • 2017-05-06
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 2018-04-09
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多