Jump to content


Photo

Release: Redirects


  • Please log in to reply
79 replies to this topic

#1 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 25 March 2011 - 02:24 PM

This module adds page to Admin -> Setup -> Redirects where you can add 301 redirects (inside your site or to other domains).

Screencast: http://www.screencas...9a-2cc70f53d2c6 (I have Chrome with Instant on so it previews pages before hitting enter.. but I'm too laze to record another screencast. Also there is sneak preview to my upcoming admin theme...)

Download & Installation: https://github.com/a...archives/master, unzip all the files to /site/modules/ProcessRedirects/ folder, check new modules from bottom of the modules page and hit install on ProcessRedirects.

More information and "tutorial" how this was done can be found from here: http://processwire.c...67.msg1071.html (and like you can see, most of the code came from Ryan's hands, I just wrapped it all together).

Hope you guys find this useful. Not tested yet on many servers, so all the feedback is more than welcome.

Attached Files



#2 Adam Kiss

Adam Kiss

    Master of the universe

  • Moderators
  • 1,129 posts
  • 328

Posted 25 March 2011 - 04:10 PM

Not to be nitpicking, but it lacks two very important things:
  • ability to process larger number of pages – say, you import 20 links and it'll take you through twenty selections of page in your tree, or creates empty redirects or something
  • ability to process non-PW links – it seems that it needs to be in /*/ format – but 0 of 30+ sites I worked on in the past two years was in this format – .htm, .php and no trailing slash were those used

As you probably understand, this is just two things, that came to my mind what would I want to do when creating redirection between old URLs and new site URLs – but nonetheless, it's good work – comes in handy when redefining structure on existing site.

A.

#3 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 25 March 2011 - 04:37 PM

Thanks for the feedback. Both are valid points.

When thinking of scope of this project (quickly to get some tool to manage redirects without polluting page tree) I am more than pleased how this turned out. Only in two days fully functional tool that works well. This said, this is very much work in progress and all the input and help is welcome.

What comes to ability to redirect urls like .html or .php that can be done and I'll fix the script a little bit to allow that.

Ability to import larger number of pages: db table is very simple, so simple sql is what I would do. But I kinda like your idea of easy importing and then also giving page selection to "Redirect to" field is nice (not only on imported urls, but always).

#4 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 25 March 2011 - 04:41 PM

I updated the download to allow urls like: /page.html, /products/product.php?id=123

#5 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 25 March 2011 - 05:45 PM

Just when going to sleep I came up with simplest solution for "importing a bunch of urls". So now there is textarea where each line behaves like different input. So you can easily import 40 urls and initially set those all redirect to /products/ page, and after that edit those to add more accurate redirects if you need to.

#6 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 373 posts
  • 48

  • LocationBratislava

Posted 26 March 2011 - 08:30 AM

amazing, i will use for sure in all of my old websites switched to PW.

I guess a good improvement could be adding a selector of pw pages in "Redirect to" with classic tree view so you can choose to write an external url or internal page.

#7 ryan

ryan

    Reiska

  • Administrators
  • 7,810 posts
  • 6604

  • LocationAtlanta, GA

Posted 26 March 2011 - 08:41 AM

Just tried it out and it works great! I will definitely use this, thanks for putting it together. Nice work!

Two minor CSS suggestions (see attached screenshots showing before and after):

/* adds padding at bottom so floated buttons don't touch footer */
#redirects_form {
        margin-bottom: 3em; 
}

/* the margin-right addition ensures the button floats all the way right */
#submit_delete {
        float: right; 
        margin-right: 0; 
}

This is such a useful module, it makes me start thinking of what more could be added... :) any thoughts on future additions? A couple ideas are: ability to upload (or paste into a textarea) a CSV file with the "from, to" urls. A counter for each redirect that keeps track of how many times it's been used.

Thanks,
Ryan

Attached Thumbnails

  • Picture 1.png
  • Picture 2.png


#8 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 373 posts
  • 48

  • LocationBratislava

Posted 26 March 2011 - 08:43 AM

@Ryan: the counter sounds great!!

#9 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 26 March 2011 - 10:46 AM

Two minor CSS suggestions (see attached screenshots showing before and after):


Heh, I didn't see those since I have alternative admin theme where these don't occur. I probably should use normal theme when developing modules :)

Counter is simple and nice idea, I will implement that soon. About csv-import: not in my needs, but this is also pretty simple, so I might implement that soon too :) Of course, if someone else wants to add that (or any other) functionality, you are welcome. Maybe I push this one to GitHub also..

#10 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 26 March 2011 - 10:50 AM

I guess a good improvement could be adding a selector of pw pages in "Redirect to" with classic tree view so you can choose to write an external url or internal page.


Ryan, any help on how to implement this page picker?

#11 ryan

ryan

    Reiska

  • Administrators
  • 7,810 posts
  • 6604

  • LocationAtlanta, GA

Posted 26 March 2011 - 12:37 PM

Here's how to implement the page picker in your Redirects module. First, give your redirect_to field an id attribute in addition to the name attribute it already has. So replace this:

$field->name = "redirect_to";

with this:

$field->name = "redirect_to";
$field->id = "redirect_to";

Now add a new field to your form, right after the redirect_to field:

<?php
                
$field = $this->modules->get("InputfieldPageListSelect");
$field->label = 'Select Page to Redirect to';
$field->attr('id+name', 'select_page'); 
$field->startLabel = "Select Page";
$field->parent_id = 0; // tell it to start from homepage

if(strpos($to, '/') === 0) {
    // if a value is already set, determine if it's a page 
    // if it's a page, then populate the input with the page_id
    $page = $this->pages->get($to); 
    if($page->id) $field->value = $page->id;
}
$form->add($field);

// now add a script that makes it automatically populate the redirect_to field
// with the URL of the selected page.
$script = <<< _END

<script type="text/javascript">
    $("#select_page").bind('pageSelected', function(event, data) {
         if(data.url.length) $("#redirect_to").val(data.url);
    });
</script>

_END;


Lastly, at the bottom of your function, we want to include the $script in the output, so replace this:

return $form->render();

with this:

return $form->render() . $script;

Let me know how that works?

Thanks,
Ryan


#12 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 26 March 2011 - 05:39 PM

Ryan: page picker works nicely.

I also submitted this to github, so future downloads from here: https://github.com/a...archives/master

#13 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 26 March 2011 - 06:39 PM

Just pushed a new version to github:

  • Ability to paste comma separated list of urls like this: redirect_from,redirect_to
  • Counter for # of redirects made (hits)
  • Page picker
  • Css problems fixed

Module needs to be uninstalled & re-installed or manually add 'counter' field to database table. Uninstall drops whole table, so be careful.

#14 ryan

ryan

    Reiska

  • Administrators
  • 7,810 posts
  • 6604

  • LocationAtlanta, GA

Posted 28 March 2011 - 09:16 AM

Great update! Thanks for your work on this.

I did run into one minor issue, and that's if the site is installed in a subdirectory, the redirect doesn't work. The reason is that it attempts to do the redirect to page's path without the subdirectory it's installed in. I just submitted a pull request to you with a possible solution. It converts PW urls to their page ID for storage purposes. This solution also ensures that redirects aren't broken if the redirect_to page is moved in PW. It only does this with redirect_to URLs that resolve to pages... it leaves all the others alone.

#15 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 28 March 2011 - 03:15 PM

Good catch and great solution. I committed your changes (git & github is great!).

I started thinking if something similar could be done with normal links (in tinymce) when using page picker? It's always bad solution if you change urls, but clients do that all the time and forget redirects... If pw could somehow keep track of page id, then it would be great! It might even remove links if you delete page that have incoming links (and warn user about this).

#16 ryan

ryan

    Reiska

  • Administrators
  • 7,810 posts
  • 6604

  • LocationAtlanta, GA

Posted 29 March 2011 - 09:13 AM

This is something I would like to add. It was in PW1 and just hasn't yet made it into PW2. I was planning to do it in combination with the tag parser, because the URLs would be a form of tag: {url, page=123}. But now that I think about it more, I'm not sure it is a good match for the tag parser, because I want tag parsing to be optional... yet I don't want URL-to-ID translation in TinyMCE to be optional. Plus, this should be relatively easy to implement... moving it up on the list. :)

Thanks,
Ryan

#17 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 30 March 2011 - 04:51 AM

Great to hear this!

UPDATE: Ryan did release this Page Link Abstractor -module (I am editing this reply from the future): http://processwire.c...opic,280.0.html

#18 Sylvio

Sylvio

    Full Member

  • Members
  • PipPipPip
  • 57 posts
  • 9

  • LocationUnited States

Posted 01 April 2011 - 03:37 AM

Hi guys,

Just downloaded the latest commit for the redirect, the one that fixes the issues when your website is installed in a subdirectory.

Found an issue, if I click the 'Add New Redirect 'button I am getting a php error:

Notice: Undefined variable: page in C:\data\My Dropbox\www\showled.com\site\modules\ProcessRedirects\ProcessRedirects.module on line 119

Notice: Trying to get property of non-object in C:\data\My Dropbox\www\showled.com\site\modules\ProcessRedirects\ProcessRedirects.module on line 119

I was able to add a new redirect, but it doesn't seem to work

The redirect needs to be from http://127.0.0.1/showled.com/about-us/ to a child page http://127.0.0.1/sho...out-us/history/

So normally I need to create a redirect from /about-us/ to /about-us/history/ is this correct?

See screenshot

Attached Thumbnails

  • 01.gif


#19 ryan

ryan

    Reiska

  • Administrators
  • 7,810 posts
  • 6604

  • LocationAtlanta, GA

Posted 01 April 2011 - 12:13 PM

Found an issue, if I click the 'Add New Redirect 'button I am getting a php error:


Looks like that is just a PHP notice because PW is in debug mode. But I did confirm there is an uninitialized var there. I think this may be my mistake. I have submitted a pull request to apeisa with a correction for this.

So normally I need to create a redirect from /about-us/ to /about-us/history/ is this correct?


I'm guessing that /about-us/ is already an active page on your site? Since you've already got a page living at that URL, that page is handling the request rather than the redirects module. To do that redirect, you'll want to edit that page's template and do the redirect from there, i.e.

<?php
$session->redirect("history/");

But that's not particularly reusable. I know on some sites, it is a common need to have a section page (like /about-us/) redirect to the first child page. If that's your need, you may just want to setup a new template called section-redirect (or something like that) and do this:

<?php
$session->redirect($page->child->url);


That way it's reusable for similar needs on any other section page.

Lastly, lets just say you wanted a generic "redirect" template that you could use on any page to redirect to any other URL. You'd want to create your template called "redirect" (example) and create a new field of type "URL", giving it a name like "redirect_href". Add your "redirect_href" field to your "redirect" template. Edit your "redirect" template to have code like this:

<?php
$session->redirect($page->redirect_href);

Then set the "about-us" page to use your "redirect" template. Edit it and type in the URL you want it to redirect too, like "/about-us/history/", in this case. Then you can continue to reuse that redirect template anywhere else that you might need an existing page to redirect elsewhere.

The value of the Redirects module is that it will handle redirects for pages that aren't in your site tree. But if the pages are already in your site tree, and you want them there, then you'd want to use one of the methods described above rather than the Redirects module.





#20 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,161 posts
  • 1712

  • LocationVihti, Finland

Posted 01 April 2011 - 01:39 PM

Looks like that is just a PHP notice because PW is in debug mode. But I did confirm there is an uninitialized var there. I think this may be my mistake. I have submitted a pull request to apeisa with a correction for this.


Actually my bug - I did some checks to collapse page picker based on the info if redirect to page is pw page or not, and I assumed $page is always available (I didn't study your code above well enough & too much work on templates). Thanks for the fix (now available from github)!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users