简体   繁体   中英

Wordpress - remove #! characters from URL

The question seems to be easy but after 3 days of searches I gave up. I thought that I will find it here and it seems to have similar ones but it doesn't work for this stuff.

Currently I have a customer that has a really advanced marketing based on his previos JS/AJAX driven website. All the back link to his website is like

SERVER /#! LINK

I have built a wordpress website and I need so that cross links open pages properly.

But if I get this link

SERVER /#! LINK

I get following URL when Wordpress process it

SERVER /

I have explored that the only way, or at least the only I know is to do it with wordpress but that doesn't seems to be easy.

I have following script that can ADD #! but is need to remove (just found it somewhere)

<?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;
    };
 ?>

Thanks to squeamish ossifrage I am almost there. I used this script as the one you provided had some issues with condition

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;
}

It seems to work but I get root page.

eg I open

myweb.com/#!thisone

and I get

myweb.com/

AND JUST TO SUMMARIZE so somebody can save lots of time - working script is

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;
    }

You can't do this in PHP because the the server will never see the fragment part of the URL (ie, the # symbol and everything that follows it).

You'll have to use client-side Javascript instead. Something like this perhaps:

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;
}

EDIT: If I understand correctly, you want to put the hash value at the end of the redirect URL. That's quite easily done. Just change the penultimate line of the above code to

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

The additional '/' may be unnecessary; I'll leave you to figure out the details :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM