WordPress - 删除#!网址中的字符


Wordpress - remove #! characters from URL

这个问题似乎很容易,但经过3天的搜索,我放弃了。我以为我会在这里找到它,它似乎有类似的,但它不适用于这些东西。

目前,我有一个客户,它有一个基于他以前的JS/AJAX驱动的网站的非常先进的营销。所有指向他网站的反向链接都像

服务器 /#!链接

我已经建立了一个wordpress网站,我需要以便交叉链接正确打开页面。

但是如果我得到这个链接

服务器 /#!链接

当WordPress处理它时,我得到以下URL

服务器/

我已经探索过唯一的方法,或者至少我所知道的唯一方法是使用wordpress来做到这一点,但这似乎并不容易。

我有以下脚本可以添加#! 但需要删除(刚刚在某处找到它)

<?php 
    $webSiteUrl = get_bloginfo('url')."/";
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    };
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    };
    if($webSiteUrl!=$pageURL){
        $pageHash = substr($pageURL, strlen($webSiteUrl), strlen($pageURL));
        header("location:".$webSiteUrl."#!/".$pageHash."");
        exit;
    };
 ?>

多亏了娇气的骨头,我快到了。我使用此脚本,因为您提供的脚本在条件方面存在一些问题

if ('' !== window.location.hash && '#!' !== window.location.hash) {
  hash = location.hash.match(/^#!(.*)/)[1];
  /* ... do something with hash, like redirecting to */
  /* another page, or loading page content via AJAX. */
  /* If all you want to do is remove everything from */
  /* the URL starting with `#!', then try this:      */
  location.href = location.protocol+'//'+location.host+location.pathname;
}

它似乎有效,但我得到根页面。

例如,我打开

myweb.com/#!thisone

我得到

myweb.com/

总结一下,所以有人可以节省很多时间 - 工作脚本是

if ('' !== window.location.hash && '!' !== window.location.hash) {
      hash = location.hash.match(/^#!(.*)/)[1];
      /* ... do something with hash, like redirecting to */
      /* another page, or loading page content via AJAX. */
      /* If all you want to do is remove everything from */
      /* the URL starting with `#!', then try this:      */
      location.href = location.protocol+'//'+location.host+location.pathname+'/'+hash;
    }

你不能在PHP中这样做,因为服务器永远不会看到URL的片段部分(即#符号和它后面的所有内容)。

你必须改用客户端Javascript。也许是这样的:

if /^#!/.test(location.hash) {
  hash = location.hash.match(/^#!(.*)/)[1];
  /* ... do something with hash, like redirecting to */
  /* another page, or loading page content via AJAX. */
  /* If all you want to do is remove everything from */
  /* the URL starting with `#!', then try this:      */
  location.href = location.protocol+'//'+location.host+location.pathname;
}

编辑:如果我理解正确,您希望将哈希值放在重定向URL的末尾。这很容易做到。只需将上述代码的倒数第二行更改为

`location.href = location.protocol+'//'+location.host+location.pathname+'/'+hash;`

额外的"/"可能是不必要的;我会让你弄清楚细节:-)