Jump to content


Photo

Redirect/Remaping URLs


  • Please log in to reply
11 replies to this topic

#1 landitus

landitus

    Distinguished Member

  • Members
  • PipPipPip
  • 96 posts
  • 6

  • LocationBuenos Aires, Argentina

Posted 27 February 2012 - 02:17 PM

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?

#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1928

  • LocationSH, Switzerland

Posted 27 February 2012 - 02:25 PM

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?

@somartist | modules created | support me, flattr my work flattr.com


#3 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 27 February 2012 - 03:33 PM

Hm... i wonder if it would be possible to add a check box on page editing to make that page being skipped on URLs... An idea for a module maybe?

#4 landitus

landitus

    Distinguished Member

  • Members
  • PipPipPip
  • 96 posts
  • 6

  • LocationBuenos Aires, Argentina

Posted 27 February 2012 - 03:34 PM

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

#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 28 February 2012 - 09:34 PM

/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?

#6 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1928

  • LocationSH, Switzerland

Posted 01 March 2012 - 06:04 PM

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.

@somartist | modules created | support me, flattr my work flattr.com


#7 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 118

  • LocationEutin, Germany

Posted 29 June 2012 - 06:15 PM

$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".

#8 SiNNuT

SiNNuT

    Sr. Member

  • Members
  • PipPipPipPip
  • 378 posts
  • 236

Posted 30 June 2012 - 02:49 AM

- 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.

#9 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1928

  • LocationSH, Switzerland

Posted 30 June 2012 - 03:15 AM

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" );
});

@somartist | modules created | support me, flattr my work flattr.com


#10 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 118

  • LocationEutin, Germany

Posted 30 June 2012 - 03:54 AM

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?

#11 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1928

  • LocationSH, Switzerland

Posted 30 June 2012 - 04:01 AM

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.

@somartist | modules created | support me, flattr my work flattr.com


#12 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 118

  • LocationEutin, Germany

Posted 30 June 2012 - 06:34 AM

I have said it before, I will say it again: Markup Simple Navigation rocks! :)

Together with a JS-based redirection to a new tab, this is very close to perfect. Thanks, Soma!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users