Forum
Remove part of the url?
Hi,
I'm using this branched setup for a multi-language site
- Home En (/en)
- Page1 En (/en/page1)
- Page2 En (/en/page2)
- Home Es (/es)
- Page1 Es (/es/page1)
- Page2 Es (/es/page2)
I have a variable $nav_group
that uses the en
or es
part of the URL to change the navgroup
depending on where the user is on the website.
$page_url = perch_page_url(['include-domain' => false,], true); // Output the URL of the current page, minus http
$url_parts = explode("/", $page_url); // Split a string by a string
$nav_group = $url_parts[1]; // Output the first part of string
PerchSystem::set_var('lang', $nav_group); // Set 'lang' for use in html templates
perch_pages_navigation(['navgroup'=> $nav_group]); // Output navgroup based on value of $nav_group
// print $nav_group; // Test output value of $nav_group
However, The default language is English. The url would look cleaner if I could hide the en
from the url. But, keep the es
and any future languages that might be added.
- Is there a 'Perch way' of removing the
en
from the url, without changing the tree structure within Perch Admin? - Or is this a task better suited for a rewrite rule in
htaccess
?
Any advice would be greatly appreciated.
Are you using Perch or Runway?
Hi,
I should of mentioned, I'm using Perch Runway.
You can add a route for each page to map one to the other.
It does make things fairly complicated though. I'd be tempted to keep things clean and simple and stick with the
/en
Hi,
Yikes, what type of horror show can I expect if I use routing like this? I'm leaning towards keeping it simple.
I've created a test route on a single page and it works if I manually type in the new route.
However the
href
in my menu is still pointing to the original URL.I can use a unique template for the
en
menu. Is there a way to removeen
from thehref
?I thought adding this might work, but it doesn't:
The HTML
a
element has noreplace
attribute - I think you want to apply that to theperch:pages
tag.There's no horror show, just lots of things to tackle - like this issue creating links.
Ah, whoops.
Combined with the route this works great.
Actual page url
site.com/en/demo-page
added a route calleddemo-page
and addedreplace="en/|"
toperch:pages
.My main menu now uses the routes instead of the url containing the
en
.Thanks