Jump to content

Release: Redirects


apeisa

Recommended Posts

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

Screencast: http://www.screencast.com/users/apeisa/folders/Jing/media/e29abcca-5ef0-44dd-ac9a-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/apeisa/ProcessRedirects/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.com/talk/index.php/topic,167.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.

ProcessRedirects5.zip

  • Like 4
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

post-1-132614277325_thumb.png

post-1-13261427734_thumb.png

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/showled.com/about-us/history/

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

See screenshot

post-218-132614277369_thumb.gif

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)!

Link to comment
Share on other sites

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.

Ryan,

You are right, about-us is an existing active page, no need to use a redirect 301 here then :)

Thanks for the quick response and the solution!

Cheers

Link to comment
Share on other sites

  • 1 month later...

In case that someone wants to use this in 2.1: it works nicely, but only for superusers.

This is how you get it working for your client (if you don't want to give superuser permission):

Create new permission: redirects-admin

Edit ProcessRedirects.module, add to line 14:

'permission' => 'redirects-admin', 

Then add that permission to roles you want to be able manage redirects. If setup page is not showing, then you need to edit admin-template and give access there also (too lazy to test this one).

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Hi,

This is a great timesaver but I'm not sure if I'm using this correctly as I get this:

Unable to complete this request due to an error

My redirect is: /contact/cv/   to  /contact/ (using the page selector)

I'm using the code off github and I'm sure that the url I'm redirecting from doesn't exist.

Regards

Martin

Link to comment
Share on other sites

I'm running 2.1 and if I don't have a page.php template file the error is:

2011-09-06 09:26:43: guest:/http404/:ProcessWire Error:Exception: Template file does not exist: '/home/site/public_html/site/templates/page.php' (in /home/site/public_html/wire/core/TemplateFile.php line 55)

If I add that template file in the error is:

2011-09-06 09:29:05: guest:/http404/:ProcessWire Error:Exception: Unknown column 'counter' in 'field list' (in /home/site/public_html/wire/core/Database.php line 72)
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
  • Recently Browsing   0 members

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