News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Search engine friendly URLs settings leads to messed up URLs

Started by MrManager, January 17, 2015, 12:43:08 PM

Previous topic - Next topic

MrManager

I would like to add a link to my template that links to the page the user is currently on, except in a different language. My current attempt is something like this:

echo '<a href="', $context['canonical_url'], '?language=spanish_es-utf8">Page in Spanish</a>';

In theory I think this should work, because canonical_url should be "index.php" on the forum index and something like "index.php/board,18.0.html" when viewing a board, which would then result in this link pointing to "index.php/board,18.0.html?language=spanish_es-utf8".

However, it looks like the friendly URL function does some sort of automatic link replacement here, so the final link that is displayed to the user points to "index.php/board,18.0?language,spanish_es-utf8.html". This obviously doesn't work.

Can I get this to work somehow?

Arantor

You can't just throw ? at the end of the link because the link it would process in that situation would be index.php?board=18.0?language=spanish_es-utf8 which isn't going to work properly.

To fix, I'd suggest something like this:
echo '<a href="', strpos($context['canonical_url'], '?') !== false ? ';' : '?', 'language=spanish_es-utf8">Page in Spanish</a>';

This means that if the ? is in the URL, use ; as a separator to get you index.php?board=1.0;language=whatever instead.

Though I'd point out that you don't even need the search engine friendly links any more - 10 years ago when it was added, sure.


Advertisement: