48

How can I go about redirecting chrome in an extension when visiting a given URL?

For example: when I visit http://yahoo.com/ I want it to redirect to http://google.com/

NOTE: A former version of this question asked whether there is any Google chrome extension which automatically redirects the tab when it visits a certain URL. Accordingly, the (currently two) answers below address different questions.

1
  • 1
    I thought I'd share this use-case which is close to this: I often right-click and copy project files and paste in the browser location to open the script. The result is something like /var/www/path/to/project/script.php, which then I have to change to localhost/path/to/project/script.php before hitting enter. I got tired of this and decided to write an extension to do the replacement for me. Mar 26, 2016 at 0:24

4 Answers 4

76

There are many options, the one more convoluted than the other.

  1. The webRequest API, specifically the onBeforeRequest event. (Even better, the upcoming declarativeWebRequest API).
  2. Content scripts. Inject location.replace('http://example.com') in a page.
  3. The tabs API. Use the onUpdated event to detect when a page has changed its location, and chrome.tabs.update to change its URL. Avoid an infinite loop though!

The first one is the best one, because it is activated before a page is even requested. The second one can be activated after the request has been fulfilled, but before the page is rendered ("run_at":"document_start") or after it's rendered ("run_at":"document_end"). I mentioned the last one for completeness, but you shouldn't use it, because the other options are way better.

Here's an example using the webRequest API, a simple extension which allows me to browse pages on the Pirate bay, even though the main hosts are taken down by my ISP (the actual list of URLs is much longer, but I have omitted them for the sake of the example).
See match patterns for an explanation on the URL formats.

manifest.json

{
  "name": "The Pirate Bay",
  "description": "Redirect The Pirate Bay to a different host",
  "version": "1.0",
  "manifest_version": 2,
  "background": {"scripts":["background.js"]},
  "permissions": [
    "webRequest",
    "*://thepiratebay.se/*",
    "*://www.thepiratebay.se/*",
    "webRequestBlocking"
  ]
}

background.js

var host = "http://tpb.pirateparty.org.uk";
chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
         return {redirectUrl: host + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1]};
    },
    {
        urls: [
            "*://piratebay.se/*",
            "*://www.piratebay.se/*"
        ],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    },
    ["blocking"]
);
20
  • this works but when I try to change the host url and reload the extension, it doesn't pick up the new url... even deleting the extension and adding it back doesn't help. I have to move the extension to a different folder and install it again... :((
    – davidhq
    Aug 11, 2013 at 11:01
  • @DavidKrmpotic Did you add the permissions for the host to your manifest file?
    – Rob W
    Aug 11, 2013 at 11:04
  • don't understand what do you mean... host (the url it will redirect to) doesn't have to be in the manifest... looks like a bug with reloading extensions. everything works with new code when I rename the folder and install the extension again.
    – davidhq
    Aug 11, 2013 at 11:06
  • @DavidKrmpotic After adding new permissions to the manifest.json file, you have to reload the extension via the "Reload" link at chrome://extensions/.
    – Rob W
    Aug 11, 2013 at 11:07
  • 3
    @Rob W I know, I was just confused for a small while why the demo wasn't working and wanted to warn future comers. Personally, I've modified it for a simple facebook redirect.
    – dmikalova
    Aug 19, 2013 at 1:34
21

I know I am a bit late in the game to answer this question Still I would like to answer this for future readers. Have a look at

Requestly - A Chrome Extension to modify Network Requests.

Currently, You can setup rules for

  1. Redirect a request URL to another url.
  2. Block some requests.
  3. Replace some part in URL with another string
  4. Modify Headers (Add/Remove/Modify Request and Response Headers)

Screenshots for more understanding:

  • List of Rules

List of Rules

  • Rule Type Cards

Rule Type Cards

  • New Redirect Rule

Creating a Redirect Rule

  • Headers Modification Rule

Modify Headers

There are lot of things in roadmap to be covered in requestly like

  • Switching User Agents

.. and a lot more.

PS: I have created this So you can blame me if you do not find this helpful :)

15
  • 2
    (I'd suggest you link to GitHub from the Chrome Extensions page, i.e. from the Overview description to the right here: chrome.google.com/webstore/detail/requestly/… to GitHub. That gives a good impression I think.)
    – KajMagnus
    Aug 17, 2015 at 8:27
  • 1
    This also solves the issue of redirecting https on youtube back to http so that we can use it through our iPrism proxy server. (And probably the best solution I have found in this regard.)
    – AdamsTips
    Feb 16, 2016 at 19:37
  • 1
    @sachinjain024 The extension is very cool, I was wondering if the code is open-source?
    – QPTR
    Apr 9, 2017 at 7:47
  • 13
    -1 The question was clearly programming-oriented, i.e. it was about how to implement redirection in one's own web extension, not about what extension to use. (Otherwise I'm sure this question would have been asked on superuser.com.)
    – balu
    Mar 16, 2018 at 0:04
  • 3
    @balu actually if you look at the question history, it started off as "is there an extension", now it no longer is like that of course Mar 24, 2020 at 21:21
0

Inssman

This chrome extension can easily redirect any URL with the simple UI

What you need is just add the URL that you want to be redirected and the URL where you want to redirect.

enter image description here

And this extension is open source https://github.com/vvmgev/Inssman Inssman also has features like

  1. Block URL
  2. Modify query parameters
  3. Modify headers of request and response
  4. Modify the response
  5. Inject the file to any web page
  6. Analyze HTTP header

enter image description here

-1

You could use my extension. Go to "Rewrite Rules" tab, click the "+" button and add a new rewrite rule. Note that the rewrite rule is actually an RegEx so characters like / must be escaped

https://chrome.google.com/webstore/detail/dev-helper/kbbgddcndpjnadfacanamniaomcohlcc?hl=en