• Resolved hilbert

    (@hilbertkruidenier)


    Hi There,
    I have a custom link domain.com/en/ (for a multi language plugin) but this link navigates automatically to a other post with the same first 2-4 character like https://www.domain.com/entry.
    Does anybody know how to fix this?

    • This topic was modified 7 years, 8 months ago by hilbert.
    • This topic was modified 7 years, 8 months ago by hilbert.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,
    Please share the specific page url that is redirecting.
    Thanks
    Mohammad

    Thread Starter hilbert

    (@hilbertkruidenier)

    Moderator bcworkz

    (@bcworkz)

    Since English appears to be the default language, why couldn’t you simply alter English links to not use the “en/” parameter at all? Thus let the English home page link be simply httрs://arabfilmfestival.nl/. I suspect the links are generated by the language plugin, but perhaps there is a filter available to alter the links?

    Another solution would be to rewrite the link with only “en/” to include the actual front page permalink. Then the canonical rewrite will rewrite that back to just httрs://arabfilmfestival.nl/.

    Third possibility is use the request filter to watch for requests where the pagename == ‘en’ and alter that to whatever the actual front page name is. Again, the canonical rewrite will change this to just httрs://arabfilmfestival.nl/.

    Thread Starter hilbert

    (@hilbertkruidenier)

    @bcworkz. The first is actually a perfect solution in this case. I should have thought of that myself … hehe.
    But I cant stand not having control of this redirect thing in wordpress. Isnt there some function to just disable the redirect?

    Moderator bcworkz

    (@bcworkz)

    Perhaps, but it may not be a good idea. When WP cannot find a matching slug for any request, it tries to find one that is reasonably close. This is a fundamental WP feature. It is how “en”, which WP thinks is a page slug, ends up becoming “entry”. I’m not sure how to disable this behavior, there probably is some way of doing so. It might be rather hacky.

    I’m assuming other links like /en/real-slug/ work correctly, so the second or third options would work with or without the /en/ argument. As mentioned, the canonical rewrite will change front page slug links back to just the domain. However, the canonical rewrite has a filter where you could change such links to httрs://arabfilmfestival.nl/en/. The filter is “redirect_canonical”. (I shouldn’t be referring to it as a rewrite, even if that is what is really happening. Last thing we need is more confusion!)

    The thing is though, I’m not sure if you return httрs://arabfilmfestival.nl/en/, that WP won’t still go and fetch “entry” again! It’d be worth a try anyway.

    In summary, I suggest you try my third option of filtering “request”, and also filtering “redirect_canonical” to return httрs://arabfilmfestival.nl/en/ for English front page slug requests. It will either work or it won’t. Either way, let us know how it goes, I’m curious.

    Thread Starter hilbert

    (@hilbertkruidenier)

    OK thanks!! so its a fundamental WP feature. good to know.
    the language plugin will skip the automatically routing to the post that is reasonably close EXCEPT the language that is set to default.

    Setting up filtering requests or redirect canonical is at this moment for me very time consuming… as a semi intermediate programmer.

    Moderator bcworkz

    (@bcworkz)

    Seeing as I was curious about this myself, I worked up the necessary code.

    // Send requests for '/en/' to front page
    add_filter('request', 'hil_chg_english');
    function hil_chg_english( $vars ) {
       if ('/en/'== $_SERVER['REQUEST_URI']) $vars = array('pagename'=>'front-page-slug');
       return $vars;
    }
    
    // Keep '/en/' parm for front page requests
    add_filter('redirect_canonical', 'hil_keep_en', 20, 2 );
    function hil_keep_en( $rewrite, $requested ) {
       if ('https://example.com/en/' == $requested) $rewrite = $requested;
       return $rewrite;
    }

    Alter the front page slug and requested URL to suit your situation. Flush your browser cache (and any server cache) before testing.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Short custom link automatically to other post with same first characters.’ is closed to new replies.