简体   繁体   中英

Issue with # in URL with a Wordpress theme?

I am having a severe problem and have no clue about what is going on... I will specify the general issue which is causing multiple issues on this Wordpress powered portal.

Steps to reproduce:

  1. Visit the URL: http://gamersgeographic.com/en/ (or any post on this site)
  2. Append #abc or #anything to the URL
  3. The URL tries to resolve for a second and magically deletes the "#" and instead changes to /abc or /anything , which of course does not exist and gives a 404 page not found.
  4. Even if the local anchor with #abc exists, behaviour is the same.

Now, consider the case below:

  1. Visit http://gamersgeographic.com/monster-hunter-diary-1/
  2. Comment link appends a #comments or #respond depending on whether a comment is there or not.
  3. Both the anchors exist on the single post page
  4. Still it redirects after finding them, to /comments and gives 404
  5. Direct URL with #comments works eg http://gamersgeographic.com/monster-hunter-diary-1/#comments works but when I change any base URL to #comments, it redirects to 404...

I have tried several combinations with Permalinks, so it is not a problem with that. I even wrote my own Comment link generator in php with just a plain href="#comments"
but still no luck...

If you need any further information about any function's code in theloop.php or anything please let me know.

Thanks in advance ! Regards

The contents of .htaccess are as below:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

This is not a PHP issue, it is Javascript: it is evident when you reproduce it, and you can test it by disabling Javascript and adding #comments at the end of the URL; it will work.

Now, I have done some work for you, and the culprit is a Javascript file aptly named hashchange.js . Look, for example, at this line:

function second_passed() { 
    if(current_page!=location.href {
        get_page_by_hash(location.href);
    }
    setTimeout(second_passed,1000);
}

Which explains why you see it “working” for a second.

And here is the redirect:

jQuery(window).hashchange(function() {
    var link = window.location.hash.replace("#", "");
    get_page_by_hash(link)
});

Note that hashchange is a method for event handling available in jQuery Mobile.

<link rel="canonical" href="URL OF YOUR HOMEPAGE HERE>  

add this in your header.php in <head></head> section and then try . it shouldn't be giving 404 error !

The way that page bookmarks are used is, as you know, the href="" of an anchor points to an #some-place. In order for this to happen #some-place must be the id of the element within the page you wish to go to.

For example:

http://gamersgeographic.com/monster-hunter-diary-1/#respond

should take you to the element with id="respond" in that page.

If the element with that ID doesn't exist you won't be able to travel to it, and may be the reason it results in a 404 Not Found. However, if the element does indeed exist on the page with the proper ID and it still redirects to a 404 then you may want to check your web server configuration to make sure it isn't filtering the # in some way.

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