Jump to content

Redirect/Remaping URLs


landitus
 Share

Recommended Posts

Hi all, I would like to create some short urls and remap others without affecting the PW tree. For instance, I have the following tree structure:

  • Products
    • Product 1
    • Product 2
    • Product 3
    • Product 100...

    [*]Categories

    [*]Industries

I would like to have the following URLs:

I've tried the redirects module, but I won't work because /site/categories/ is a valid URL already. I wouldn't want to nest Categories and Industries in the tree because I have 100 products in Products.

What can I do? Should I create a proxy? Like here ? Is there a clean/preferred way?

Link to comment
Share on other sites

Easiest would be to use $session->redirect(url); to redirect. Not sure what exactly you're trying to archive. Do you really want to redirect a longer url to a shorter one? Or do you want display same content within another url?

Link to comment
Share on other sites

I'm trying to Remap some URLs without affecting the Processwire tree structure. The correct URL should be http://site/products/categories but, because "products" has 100 children, I have "Categories" outside the "Products" parent. Now I want to remap /site/categories/ to http://site/products/categories

Tell me if I'm making any sense

Link to comment
Share on other sites

/site/templates/categories.php

<?php
if($input->urlSegment1) {
   // you'll have to enable urlSegments in your categories template
   $s = $sanitizer->pageName($input->urlSegment1); 
   $category = $pages->get('/products/categories/$s/'); 
   if($category->id) echo $category->render();
       else throw new Wire404Exception();
}

Let me know if this is what you were looking for?

Link to comment
Share on other sites

I think landitus means the other way round. :)

Edit:

http://domain.com/products/categories >>> goes to /site/categories/

http://domain.com/products/industries >>> goes to /site/industries

By "goes" to, do you mean it redirects to it? Like if you enter the long address it instead loads http://domain.com/categories/ also in the address bar? And simply show the /categories page?

I think I'm not getting it still.

Link to comment
Share on other sites

  • 3 months later...

$session->redirect(url);

I use this (together with a URL field als the only field of a special template) for "external" links in the navigation. Works like a charm.

Now the client would like these links to open in a new tab in order to keep their web site open. Any chance to do this with $session->redirect in a sensible way? Obviously, I do not want to use target="_blank".

Link to comment
Share on other sites

- snip -

Obviously, I do not want to use target="_blank".

I don't think this is a bad choice, if you want to open external links in a new tab (or window, depending on what the user's browser and settings).

In html5 the "target="_blank" isn't deprecated.

Link to comment
Share on other sites

There's no way to do this in php, this has to be done in html or js client side. There's nothing against using target blank in HTML5.

If you don't want to, you could add rel="ext" to the links and use jquery to make them behave like you want. This technique was often used in XHTML where target blank wasn't valid.

Add it using javascript

$("a[rel='ext']").attr("target","_blank");

Or using only the protocol, with any link that href starts with http://

$("a[href^='http://']").bind("click", function() {
   window.open( $(this).attr("href"), "_blank" );
});
  • Like 1
Link to comment
Share on other sites

This would have been my first choice since I want to open the new tab using JS anyway, however it doesn't work on those redirected links in the navigation since the href attribute of those is still a local link.

The navigation is created using Markup Simple Navigation. For any reference to an external page, I have a template page-redirect.php, which contains just

<?php
// Internal or external redirects in primary nav
if($page->redirect_url) $session->redirect($page->redirect_url);

This template only has the fields title and redirect_url. So if I had a page /markup/navigation/ with the redirect_url set to http://processwire.com, the link href in the navigation markup would still be /markup/navigation/. It's redirected after the link is clicked which is why the JS solution won't work.

Can I maybe add code to Markup Simple Navigation's item_tpl using template selectors?

Link to comment
Share on other sites

Ah that's something different. You could try something like this:

'item_tpl' => '<a href="{redirect_url|url}">{title}</a>'

Then it will take the redirect_url if it's populated.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...