Wordpress Themes

A place for my programming projects and the occasional blog about technology related matters.

Redirector

Source code - addons.mozilla.org page - Version history

This was done as a request for someone on the Mozillazine forums.

The extension automatically redirects to user-defined urls on certain pages. You can specify patterns for pages that should be automatically redirected to other pages. E.g. you always want http://example.com/foo.html to redirect to http://example.com/bar.html. Supports both wildcards and regular expressions in patterns. Regular expressions support capturing parantheses so you could define the pattern: http://foo.com/bar.php\?id=(\d+) and define the redirect as http://bar.com/baz.php?id=$1. In that case http://foo.com/bar.php?id=1234 would automatically redirect to http://bar.com/baz.php?id=1234. With wildcards $1 will map to the contents of the first star, $2 to the second star and so on.

This can be useful for instance to skip confirmation pages after posting messages on message boards, skipping ad pages that appear before you can view content on certain sites, redirecting from http to https on sites where you always want the https version, or just anywhere where it takes you two or more clicks to get to what you want.

Previous versions supported XPath expressions in the redirect url but version 1.5.2 and later versions don’t. The reason is that as far as I know no one was using the feature and it was causing some problems so I decided to remove it.

As of version 1.0 there are two types of redirects, fast and slow. Previous version always redirected when the original page had loaded. So if you wanted to redirect from page X to page Y, you first saw X load and then it went to Y. That’s what we call a slow redirect. In version 1.0 you won’t see X load, when you click a link for X you will go right away to Y. These are called fast redirects. There are two notable exceptions that still use slow redirects. They are:

1. Redirects where ‘Only if link exists’ is specified. In this case we have to load page X first to see if the link exists and so can’t do a fast redirect.

2. Post requests. If you’re posting to page X then we can’t do a fast redirect because then the POST would never happen. For instance, if you were logging in to page X with a POST method and wanted to redirect to page Y after the login, then if we had a fast redirect the POST would never happen and you wouldn’t be logged in. Post requests need to use slow redirects so the web will continue to function as expected.

To add a new redirect, right click either anywhere on a page and click ‘Add current url to Redirector’ or right click on a link and click ‘Add link url to Redirector’. You can also go to Tools -> Redirector -> Add and add a new url there, but then you won’t get the example url filled in.

To edit and delete redirects, goto Tools->Redirector, or choose Options on the Addons window.

To disable Redirector temporarily, click on the R that’s in your statusbar.

Works with: Firefox: 2.0 – 3.0b2
Latest version: 1.5.2

Download

Reader's Comments

  1. pandaking |

    Hey, just saying thanks for making this.
    Loving it =)

  2. Dave |

    This add-on is nice to have, but I still can not figure out how to use regular expressions with it, even if I enter everything as explained here on the page.

    I use firefox 2.0.3.

    I tryed using the following urls
    index.php%3Fref%3D357.51162346
    index.php%3Fref%3D357.(\d+)
    index.php%3Fref%3D357.$1

  3. einar |

    Hi Dave

    The problem you have is because the redirect url is a relative url, not an absolute url. The extension uses simple javascript redirection, i.e. it redirects to the new page after the old one has loaded but before it is rendered, and javascript document.location = ‘index.php’ doesn’t seem to work. Try this:

    Example url: index.php%3Fref%3D357.51162346
    Pattern:     index.php%3Fref%3D357.(\d+)
    Redirect to: http://tech.einaregilsson.com/1
    

    and then try going to http://tech.einaregilsson.com/index.php%3Fref%3D357.51162346 and see if you aren’t redirected. I probably should have mentioned in the documentation that the redirect url must be absolute. You probably should have the pattern absolute too, but that’s up to you, the pattern will work either way, it’s just the redirect url that must be absolute.

    Hope this helps,
    -einar

  4. hkBattousai |

    I don’t know how to use regular expressions, so please help me with this,

    I want to add &comments=all to the end of all “http://www.haber7.com/haber.php?haber_id=267167″-like addresses. As you could guess the decimal number right after “id=” can take any value.

    For example, I want to convert,
    “http://www.haber7.com/haber.php?haber_id=123456″
    to
    “http://www.haber7.com/haber.php?haber_id=123456&comments=all”

    How can I do that?

  5. einar |

    Hi

    Try these:
    Example url: http://www.haber7.com/haber.php?haber_id=123456
    Pattern: http://www.haber7.com/haber.php\?haber_id=(\d+)$
    Redirect to: http://www.haber7.com/haber.php?haber_id=$1&comments=all

    That should work for you. And of course remember to select ‘Regular Expression’ instead of ‘Wildcard’.

    Small breakdown of the regex in case you’re interested:

    1. The \? instead of just ? is because ? is a special character in regex and by using the escape character \ we say ‘Match an actual ? sign’.
    2. (\d+) The brackets () mean to capture what’s inside them so you can use it in the replacements (where it’s defined as $1 or the first capture). The \d means a digit, and + means a digit one or more times.
    3. $ means end-of-string. This is because otherwise the pattern would also match http://www.haber7.com/haber.php?haber_id=123456&comments=all and would constantly try to redirect again. So we say ‘the url must end after the digits’.

    Hope this helps.
    -einar

  6. Jeff Daly |

    Would it be possible to get Firefox to preload a page in the Forward History instead of redirecting? There are several sites, where upon visiting a page, I nearly always click a certain link after reading the content, or type in a specific URL based on the content.

    For example, on http://boingboing.net/ posts, I like to read the content posted by the BoingBoing contributor, and then click on the link to the page they are referencing - usually it is labeled “Link”. It would be useful if Firefox automatically loaded “Link” as the next page in my forward history as soon as I visit a page like:

    url: http://www.boingboing.net/2007/09/19/skull-font-free-from.html
    regex: http://www.boingboing.net/\d+/\d+/\d+/

    so now have Firefox preload the link found at XPath: /html/body/div[@id=’wrap’]/div/div/div[2]/div/p/a

    Another idea, could be to select some text via XPath and then pass it on to a Firefox bookmark that has a keyword. For example, after reading about a movie like “The Matrix” on imdb.com, I often go to the URL toolbar and type in:

    imp “the matrix”

    which I have bookmarked as a keyword for:
    http://www.google.com/custom?domains=www.impawards.com&q=%s&sitesearch=www.impawards.com

    I’m then redirected to a search on the Internet Movie Poster Awards site which usually has movie poster images of the largest size.

    Just a few ideas.

  7. einar |

    Those would all be possible but I’m not sure I wanna take it in that direction. (By that I mean that I’m way too lazy to implement it :) ). But those are good ideas and might be worthy of their own extension. But I’m pretty sure there are some pre-fetch extensions out there that might be modified to include this functionality.

  8. hkBattousai |

    Don’t be lazy :) , this extension can be one of the best and useful one if you solve this redirection problem.

    Me too think that it would be best if it opened only the page to be redirected, instead of first loading the original page and then redirection to another.

    Anyway, i like this extension, good work! :)

  9. einar |

    Well hkBattousai, you’re in luck. Version 1.0 which I just released adds instant redirects.

    And Jeff, while I didn’t do the pre-fetching thing I liked the xpath idea. So in v1.0 you can create the redirect url from an xpath expression :)

  10. Jeff Daly |

    You’ve added XPath expressions?! Sweet! Thanks einar.

  11. Leo |

    Hey there,

    Thanks a lot for the time and effort dedicated to the extension. I’m really sorry, but I can’t figure what I’m doing wrong:

    I would like to use the string “950305″ to be redirected from
    http://www.lanacion.com.ar/informaciongeneral/nota.asp?nota_id=950305&pid=3297495&toi=5234
    to
    http://www.lanacion.com.ar/herramientas/printfriendly/printfriendly.asp?origen=3ra&nota_id=950305

    And this should also work in other sections of the same website. For example:

    I would like use the string “950398″ to be redirected from
    http://www.lanacion.com.ar/weblogs/rugby/nota.asp?nota_id=950398
    to
    http://www.lanacion.com.ar/herramientas/printfriendly/printfriendly.asp?origen=3ra&nota_id=950398

    So… could you please give me a hand?

    Thanks,

    Leo

  12. einar |

    Hi. For this you’d have to use a regular expression match, to be able to capture the number defined by nota_id= and use it in the redirect url. For your first example you should be able to do this:

    example url: http://www.lanacion.com.ar/informaciongeneral/nota.asp?nota_id=950305&pid=3297495&toi=5234
    pattern: http://www.lanacion.com.ar/informaciongeneral/nota.asp\?nota_id=(\d+)&pid=\d+&toi=\d+
    redirect to: http://www.lanacion.com.ar/herramientas/printfriendly/printfriendly.asp?origen=3ra&nota_id=1
    Remember to check the ‘Regular Expression’ box.

    For the second one you should be able to do this:

    example url: http://www.lanacion.com.ar/weblogs/rugby/nota.asp?nota_id=950398
    pattern: http://www.lanacion.com.ar/weblogs/rugby/nota.asp\?nota_id=(\d+)
    redirect to: http://www.lanacion.com.ar/herramientas/printfriendly/printfriendly.asp?origen=3ra&nota_id=1
    Remember to check the ‘Regular Expression’ box.

    Now, note that if they are sometimes different, e.g. sometimes there an additional parameter &x=y in the url or something like that, then this will not work for all cases. But you said you wanted to use it on other section to. For the general case where you want ALL urls that end in nota.asp? and have a nota_id parameter to redirect to the printfreindly url, you could do something like this:

    pattern: http://www.lanacion.com.ar/.*?/nota\.asp\?.*?nota_id=(\d+)
    redirect to: http://www.lanacion.com.ar/herramientas/printfriendly/printfriendly.asp?origen=3ra&nota_id=1

    This should work on both the example urls you provided.

    Let me know if you have any additional questions.

  13. Leo |

    Wow, that was a quick response. Brilliant, it works perfectly. I really really appreciate it. Additional questions? Yes, two more annoying ones:

    1) It seems that when the add-on is enabled (within Firefox’s add-ons menu, not the Redirector’s bottom right “R” switch), for some reason Firefox keeps displaying the “Waiting for” message at the bottom left, as well as the loading progress bar on the right. This occurs in any webpage, regardless of whether Redirector is working on it or not.

    2) Do you think you’ll be able to make the add-on work as well when the link to be redirected is opened in a new tab?

    Anyway, they’re just a couple of observations, no complaints from my part whatsoever, I’m very grateful for your work. Thanks again!

    Leo

  14. einar |

    1) The ‘Waiting for’ stuff is a bug that I already got a bug report for. I fixed it in Redirector 1.0.1 but it’s being held for review at addons.mozilla.org because there was some problem with the icon in Linux or something. However, you can get 1.0.1 on this page, at the top after the extension description there is a download link for 1.0.1. I haven’t had time to fix the icon Linux stuff so it might be a few days till you can get it at addons.mozilla.org.

    2) That *should* work! (Famous last words…). But I haven’t tested it especially, might be something that keeps it from working when opening in a new tab. I’ll look into it for the next release.

  15. hkBattousai |

    I think there’s an error in the new version.
    It started to give this error :
    http://img172.imageshack.us/img172/8408/errorud6.jpg

    It shouldn’t do recursion for that string. The regular expression is “…(/d+)”. And my URL is “…(/d+)&comments=all”. They don’t match, do they?

  16. einar |

    …(/d+) matches …(/d+)&commets=all because ending with (/d+) doesn’t say that it has to be the end of the string. Just like ‘mike’ would match ‘mikelle’. So, add $ to the end of your pattern and then you’re saying it will only match something that ends with (\d+)

  17. Peuj |

    Hi,

    Some suggestions:

    1- Redirector only works when use left click on the link (to open the web site) or using “Duplicate tab” menu. It doesn’t work when I use the right-click “Open Link in new tab” or if I use the middle click to open the link in a new tab.

    2- It could be nice to have access to the Redirector Settings panel from the icon in the status bar and not only from the Tools menu.

    3- It could be nice too to have an option to choose if we want to see or not “Add current url in Redirector” in the context menu.

    Thanks

  18. Leo |

    Hello again…

    I can’t figure how to introduce directories as the variable.

    I would like to be redirected from:
    http://www.macworld.com/2007/10/secrets/tcoleopardupgrade/index.php?lsrc=mwrss
    to:
    http://www.macworld.com/2007/10/secrets/tcoleopardupgrade/index.php?pf=1

    Help please…?

    Thanks

  19. einar |

    @Peuj: Thanks for the suggestions. I’ve alread gotten some complaints about the right click thing, that’ll definitely be in the next version. I’ll probably also add the settings menu from the statusbar icon. As for disabling the context menu item, I might add it as a preference you can set in about:config, I doubt I’d bother to do a preferences dialog unless I start coming up with some new prefs. I’m not sure when the next release will be however, maybe in a few days or weeks.

    @Leo: I’m not sure what you mean. I’m guessing you want all the index.php?lsrc=mwrss to be index.php?pf=1 instead. But I don’t know which of the directories are variables. Obviously 2007 and 10 are, but what about secrets and tcoleopardupgrade? Is that the name of this particular article, or category or what? The quick and dirty way to do this would be to say something like:

    Pattern: http://(www.)?macworld.com/(.*)/index.php\?lsrc=mwrss
    Redirect url: http://www.macworld.com/2/index.php?pf=1

    Which just means that any url on macworld.com (with or without a leading http://www.) that ends in index.php?lsrc=mwrss will redirect to the same url, but with ?pf=1 instead.

  20. jon green |

    Fast redirects don’t seem to be working from the address bar, or from a live bookmark, for me.

    My pattern is
    http:\/\/www\.anandtech\.com/?\w*\/showdoc\.aspx\?i=(\d*)
    to redirect to
    http://www.anandtech.com/printarticle.aspx?i=1

    “regular expression” is selected, “only if link exists” not selected, but I see the page load before the redirect occurs if I navigate to, e.g. http://www.anandtech.com/cpuchipsets/showdoc.aspx?i=3134 from a live bookmark or pasting straight into the address bar. If I follow a link from the homepage, however, I do get a fast redirect.

    Still, it’s faster than clicking a bookmarklet to get where I want to, so many thanks for the extension!

    jon

  21. einar |

    Hi Jon

    There are some cases where fast redirects don’t work and some cases where redirects apparently don’t work at all, like middle clicking a link or right clicking and choosing ‘open in new window’. The plan for the next version is to try to fix all the edge cases, but I haven’t even started working on it yet, it won’t be out for at least a few weeks.

    -einar

  22. Netcat |

    Very useful extension, thank you….
    …but does not work reliable, making it useless :-(

    (The redirected tab needs to have focus, otherwise Redirector ignores the matching URL).

    Waiting hopefully, maybe you’ll find the time to fix this extension.

  23. Eden |

    Hi,
    Very useful extension, thank you….
    but i have problem, I need that the redirect will suspended because i want
    the original page will be load and i will see the page for 3 or 4 seconds.

    can it possible?

  24. Olivier |

    Hi,
    I try to redirect my links:
    For instance from:
    http://www.jneurosci.org/cgi/content/short/27/51/14049
    To
    http://www.jneurosci.org.gate2.inist.fr/cgi/content/short/27/51/14049

    In a more generic way that would be to redirect anything from
    http://www.jneurosci.org
    to
    http://www.jneurosci.org.gate2.inist.fr
    Could you help me?

  25. einar |

    Hi Olivier. Try this:

    Set pattern type as Regular expression

    Pattern: http://www.jneurosci\.org(.*)

    Redirect to: http://jneurosci.org.gate2.inist.fr1

    Let me know if that doesn’t work, then I’ll figure out a pattern that definitely works. I just don’t have time to test it right now :)

    p.s., you could even allow to redirect from urls without www in front by doing:

    Pattern: http://(www.)?jneurosci\.org(.*)

    Redirect to: http://jneurosci.org.gate2.inist.fr

  26. Olivier |

    Thanks Einar for your fast reply!

    Your code didn’t work as you wrote, but it allowed me, turning around, to find the proper syntax:
    Pattern:
    http://www.jneurosci.org/cgi\/(.*)
    Redirect to:
    http://gate2.inist.fr/login?url=http://www.jneurosci.org.gate2.inist.fr/cgi/1

    Is there / will be a possibility to save the settings for backup or sharing ?
    My collegues will be very happy with this excellent addon!

  27. einar |

    I don’t really understand why you want to redirect all your pages to the login page. Couldn’t you just as well have the redirect to:

    http://www.jneurosci.org.gate2.inist.fr/cgi/1

    Anyway, about importing/exporting settings, yours is the first request I’ve gotten for that, so it’s not really on the drawing board yet. However it is a good idea and so might be implemented at some point in the future. For now you can goto about:config in your browser window, type ‘redirector’ in the filter window and then you should see the key: extensions.redirector.redirects

    The value from that are all your redirects, so you should be able to just copy that string and paste it into the same key in another instance of firefox.

  28. shinemoon |

    Just come here to say thanks to you for such a cool add-on! And Merry Christmas:)

  29. shinemoon |

    Hi Einar,
    I am using your add-on, but I found there is a minor weird thing.
    If I click the link which will cause a new tab(e.g ‘open in a new tab’ or use some tab add-on like Tab Mix, and in fact most web sites open their links in new pages by default in China :)…. ), the redirector sometimes can’t work.
    Hope you can check this after your back from holiday:)Thanks!

  30. Moita |

    Hi, I’m trying to redirect this:

    http://meudominio.com/*/
    where * means any page possible
    to:

    http://meudominio.com/*/ok
    but this ‘*’ must be the same of the other one. Can I do that?

    Eg:
    http://meudominio.com/alo/ to http://meudominio.com/alo/ok
    http://meudominio.com/bomdia/ to http://meudominio.com/bomdia/ok

    I need a single rule to do that.
    Thank you!!!

  31. Moita |

    Ok, i did it. Thank u anyway!

  32. jon green |

    Until Einar has time to add the preference to remove the context menu item, you can hide it (and/or the statusbar icon which I also prefer not to have) by adding the following line(s) to your userChrome.css file:

    #redirector-context{display:none !important;} /* hide redirector context menu items */
    #redirector-status{display:none !important;} /* hide redirector statusbar icon */

  33. einar |

    Hi Jon

    You’ll be happy to know that I’ve added about:config entries for both those things in the latest version which should be out in a few days, a couple of weeks at most.

    Other planned changes:

    * Redirects work when opening new windows, tabs, from bookmarks, etc.
    * Possible to specify “Exclude patterns” to simplify some patterns that were hard to express with a single regex.
    * Better UI for the redirects window.
    * Help file with some common examples of redirects.
    * Menu available from the statusbar icon.

    Basically I put in almost every suggestion I’ve gotten in the last few months. It should be out pretty soon.

  34. shinemoon |

    Einar,Happy new year! And happy to heard your great changes in plan:)

  35. Kmaid |

    I have found a great use for Redirector for those of us who like OpenDNS and HATE the search they put us on instead of doing an im feeling lucky on google.

    Example URL: http://guide.opendns.com/?url=zomg
    Pattern: http://guide.opendns.com/\?url=(\w+)
    Redirect to: http://google.com/search?btnI=1&q=1

    Using Regular Expressions
    It is a little slower than it would be normally but well worth it in my opinion try it out!

  36. Kmaid |

    Small bug report, The redirector only works in the active window. If a page page is redirected in a tab you are not viewing the rules are not applied -kaet

  37. einar |

    Version 1.5 is now out which fixes all the known bugs and adds some new features. It’s currently in the sandbox at AMO but you can get it here on this page and read the release notes to see what’s new.

  38. shinemoon |

    Cool~

  39. Peuj |

    Thanks !!

  40. NetCat |

    Thank you !!

    http://netcat.ath.cx/extensions.html

  41. Tarquin |

    Hi Einar,

    I’m not sure whether this is a bug report or a feature request, but Redirector doesn’t seem to work with images loaded as part of another page. It *does* work if the images are loaded directly.

    For instance, if you have a rule that redirects http://serv1.example.com/* to http://serv2.example.com/1 (ie, anything on that host), and you visit http://www.example.com/, which loads http://serv1.example.com/foo.jpg in an IMG tag, the request is not redirected: the image loads from serv1 anyway. If you load the image directly (eg, by right-clicking on it and selecting “View Image”), the request is redirected successfully and the image loads from serv2 as expected.

    I’d expected this to be the same sort of problem that affected redirections in new tabs and so on, but I see that’s been fixed; it would be really nice if this case could be fixed too. Thanks!

  42. Tarquin |

    Hi Einar,

    (apologies if this is a duplicate post; my posts seem to be getting lost.)

    I’m not sure whether this is a bug report or a feature request, but Redirector doesn’t seem to work with images loaded as part of another page. It *does* work if the images are loaded directly.

    For instance, if you have a rule that redirects http://serv1.example.com/* to http://serv2.example.com/1 (ie, anything on that host), and you visit http://www.example.com/, which loads http://serv1.example.com/foo.jpg in an IMG tag, the request is not redirected: the image loads from serv1 anyway. If you load the image directly (eg, by right-clicking on it and selecting “View Image”), the request is redirected successfully and the image loads from serv2 as expected.

    I’d expected this to be the same sort of problem that affected redirections in new tabs and so on, but I see that’s been fixed; it would be really nice if this case could be fixed too. Thanks!

  43. einar |

    Hi Tarquin.

    Your posts are not getting lost, it’s just that any comment with more than one url in it needs to be approved to reduce spam. Spam comments usually have 10-20 links in them.

    About your question, Redirector doesn’t support image redirecting, it will only redirect pages. You are the first person ever to request this, I might look at it in a future release but it won’t be anytime soon, sorry.

  44. DeathWolf |

    any chance you could make this also affect urls manually entered in the urlbar?

  45. einar |

    @DeathWolf: That should work. Can you give me an example of a pattern and url where it doesn’t work for you? And your Redirector version and Firefox version?

  46. DeathWolf |

    redirecting something like ‘^(site:.*)’ to ‘http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=$1′ (to avoid firefox’s stupid failure in case of non-recognized protocol)

  47. einar |

    Ah, now I see. That doesn’t work because I only get notified of a change in url when it’s changing to a known protocol. The error message you get is produced before I get a change to rewrite the url, so it won’t work. To change it I’d have to capture some event that occurs even sooner then the location change thing. I might look at it in a future version, but right now I’m unfortunately way too busy to work on this extension :(

  48. DeathWolf |

    No problem:) I have so far been using a modified URLFix(another extension) to do it, so I was just asking to see if you had time for it:)

  49. Fujilives |

    Thanks for this addon!

    I used this to direct mail.google.com/mail to mail.google.com/mail/h (basic html mode).

    Now whenever I check my email I get to do it in basic html mode instead of the bulky javascript-intensive classic view.

    Here are the lines I used should anyone else out there want to use this addon to make Basic HTML view in google mail your default mail view upon a regular gmail login via mail.google.com!

    Include: mail.google.com/mail(.*)
    Exclude: mail.google.com/mail/h(.*)
    Redirect: https://mail.google.com/mail/h/

    I hope this helps someone!

  50. Nadia |

    Hi all.. i kinda new using this extension.. is it possible to redirect from a post function..?? if can, can sumone show me how..?

    another thing i kinda bored ticking some of the radio buttons also.. is it possible to use redirector when a page loads, we can set which radio button to check.. if can not can someone make add-ons like that also.. hehe.. i know u guys are damn good with progamming.. hope u can teach me sum..

    Thx

  51. einar |

    Hi Nadia. Sorry, redirecting from post is not possible. The availability to set form values before redirecting is interesting but I doubt it will get into the extension any time soon since I don’t have much time to work on it.

  52. Jon Green |

    A request for when you next get time to work on this extension - an option to send the original URL as referrer to the new URL. It seems that some sites display their home page rather than the page requested, if you haven’t first been to the original URL.

CommentComment

For spam detection purposes, please copy the number 4519 to the field below: