Forum

Thread tagged as: Question

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.

  1. Is there a 'Perch way' of removing the en from the url, without changing the tree structure within Perch Admin?
  2. Or is this a task better suited for a rewrite rule in htaccess?

Any advice would be greatly appreciated.

Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you using Perch or Runway?

Hi,

I should of mentioned, I'm using Perch Runway.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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,

It does make things fairly complicated though

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 remove en from the href?

I thought adding this might work, but it doesn't:

<a replace="en/|" href="<perch:pages id="pagePath" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

The HTML a element has no replace attribute - I think you want to apply that to the perch: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.

<a href="<perch:pages id="pagePath" replace="en/|" />

Actual page url site.com/en/demo-page added a route called demo-page and added replace="en/|" to perch:pages.

My main menu now uses the routes instead of the url containing the en.

Thanks