Support » Developing with WordPress » Single aliasing for URLs plugin

  • Salvatore

    (@scapolupogmailcom)



    Hello,

    I am working on a custom WordPress + Woocommerce, and having troubles about redirect and/or aliasing of translated URLs of products. For better understanding, please suppose you have a multilanguage website, where URL alias are stored as attributes for each posts, ie:

    $post_id = 123;
    $url_default = get_permalink( $post_id );
    $url_es = get_post_meta( $post_id, ‘url_translation_es’, true );
    $url_fr = get_post_meta( $post_id, ‘url_translation_fr’, true );

    Clearly I need to manage this via code, because plugin we used for URL translation has no support for it (qTranslate). so we need to rewrite in most simple way. Here come the pain: WordPress seems to have support for URL rewriting and NOT for aliasing, which is slightly different as meaning and behaviour.

    Redirect (for example using add_rewrite_rule) is NOT good, because it changes destination final URL:

    Be site.com/just-test our base URL, with post_id=123, and site.com/test-spanish its spanish translation. Now user try to open:

    site.com/test-spanish

    and need to go in postid=123 without changing its url, so in my POV this is aliasing, not redirecting. It is the best approach? Is it possible doing this, or too much complicated / heavy load? Do I need to add alias in htaccess or just PHP code is ok?

    I would need to do this for each post_id inserted, i.e while creating every single product via import.

    Hope is all clear. Thanks in advance

    • This topic was modified 2 months, 2 weeks ago by  Salvatore.
    • This topic was modified 2 months, 2 weeks ago by  Salvatore.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    If a request goes to a different destination, it’s a rewrite, whether the address bar changes or not. Relative rewrites do not change the address, absolute rewrites change the address.

    All the various language requests for a post should all lead to the same post? The translation of the post is handled? In other words, there are not separate Spanish and French posts that are translations of the original post language?

    You really need something in the URL that WP can use to distinguish between a translated post slug and the post’s actual slug. For example, the “alias” slugs might be all be preceded with “/lang/”, or instead use either “/fr/” or “/es/”. Even easier would be to add a query string after the permalink. Some examples that would all lead to the same post:
    example.com/?p=123
    example.com/hello-world/
    example.com/lang/bonjour-le-monde/
    example.com/es/hola-mundo/
    example.com/hallo-welt/?lang=de

    This is because the query must be changed to find a post by a meta value instead of its actual title slug. Actually, what would be easiest is to simply supply the post ID regardless of what the request is.
    example.com/ciao-mondo/?p=123

    Involving altered permalinks like example.com/es/hola-mundo/ requires adding rewrite rules within WP (not .htaccess). The rule will “see” the “/es/” or “/fr/” element and rewrite to index.php?meta_key=url_translation&meta_key=hola_mundo

    Adding something like ?lang=es requires hooking the “pre_get_posts” action, verifying that a “lang” query var exists and if so, altering the query vars to get a post by post meta instead of title slug.

    Adding something like ?p=123 doesn’t require any request handling, you only need to manage link output.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.