Redirect URLs in Chrome?

38

19

Is there any extension to Chrome that will let me force a URL from a particular domain to be redirected to another domain?

(E.g. Redirect http://www.google.com to https://encrypted.google.com.)

Note: I'm looking for an arbitrary redirector, not KB SSL Enforcer, which only works for the specific task of redirecting to HTTPS.

user541686

Posted 2011-05-16T07:18:26.563

Reputation: 21 330

1

I had no idea https://encrypted.google.com existed! Cool

– tony19 – 2015-10-09T20:31:46.727

You could also consider using the HOSTS file as another method. – Synetech – 2011-05-23T20:33:34.550

@Synetech: Would that redirect just the root page, or anything with a particular domain? – user541686 – 2011-05-23T20:39:59.157

It would redirect the whole domain (or subdomain as the case may be). – Synetech – 2011-05-24T02:53:20.027

@Synetech You're confusing redirecting with resolving, changing the IP address that an address resolves to will not work in this case. He wants to redirect to a different domain, not try to load that domain name on the wrong web server. – Justin Buser – 2012-06-26T09:19:37.253

@JustinBuser, > You're confusing redirecting with resolving   No, I'm, not. Under certain circumstances, there is no difference. I use the HOSTS file to redirect all ads to a local web-server that is set up to accept any URL and replace pages with a small tiny bit of HTML that simply says [ad] and pictures that are a single transparent pixel. In other words, the ad sites are redirected to localhost (though they easily could be redirected to another machine). – Synetech – 2012-06-26T15:43:11.437

@JustinBuser, > changing the IP address that an address resolves to will not work in this case. He wants to redirect to a different domain, not try to load that domain name on the wrong web server   He has not indicated what the actual domains he wants to redirect from/to are (he used Google as an example), so it may or may not work. That's why I said he could consider it as a comment and not an answer. – Synetech – 2012-06-26T15:46:35.847

@Synetech No, I'm, not. Under certain circumstances, there is no difference You're just wrong, they are two COMPLETELY different things. Putting an entry in your hosts file pointing Domain A to IP X changes what Domain A RESOLVES to, that does not REDIRECT anything because it never goes to the original IP. REGARDLESS, that WOULD NOT solve anything, putting an entry for www.google.com with the IP address for encrypted.google.com into your hosts file would result in Chrome trying to load www.google.com from the wrong IP, it would not REDIRECT (i.e. the address would remain www.google.com) – Justin Buser – 2012-07-15T14:35:50.073

1@JustinBuser, it would redirect the browser from the original IP to the specified IP. Imagine digging a ditch to re-route water. You dig a ditch, open the dam, then the water goes to the new spot instead of where it would have gone. I never said anything about 302, so you are only arguing semantics. – Synetech – 2012-07-15T17:03:46.133

Answers

32

I had built a Chrome extension which does this.

Note: I built this for just 2 sites - just for the heck of it - by no means it's professional quality™. Please don't flame me for crappy code :)

Edit: Updated to manifest v2, which brings in certain additional restrictions.

manifest.json

{
  "name": "URL Redirect",
  "version": "0.2",
  "description": "Checks URL and redirects as required.",
  "background": { 
     "page":"bg.html"
     },
   "manifest_version": 2,
   "content_scripts": [
   {
     "matches": ["http://*/*", "https://*/*"],
     "js": ["content.js"]
   }
   ],
  "permissions": ["tabs"]
}

bg.html

<html>
  <script src="redirect.js"></script>
</html>

redirect.js

chrome.extension.onRequest.addListener(function(request, sender) {
        chrome.tabs.update(sender.tab.id, {url: request.redirect});
    });

content.js

var pattern=/\bBlocked/;
var viewtext_base_url = "http://viewtext.org/article?url=";
var newurl;
if (pattern.test(window.document.title)) // if it matches pattern defined above
{
  newurl = viewtext_base_url + encodeURIComponent(window.location.href);
  chrome.extension.sendRequest({redirect: newurl}); // send message to redirect

}

To install this, create files with filenames as mentioned above the codeblock.

enter image description here

Once all 3 files are created, Click on Chrome Menu → Tools → Extensions. Click the "+" on Developer Mode. Click on Load Unpacked extension and point to the directory where the files are stored.

enter image description here

Edit the files are required, and uninstall and reinstall the extension as mentioned above

Sathyajith Bhat

Posted 2011-05-16T07:18:26.563

Reputation: 58 436

Can it be used to edit the URL of CSS/JS the webpage is trying to load? – Nakilon – 2015-03-26T22:46:08.777

@Nakilon no, that wont be possible – Sathyajith Bhat – 2015-03-27T03:56:18.987

Although this looked promising, none of this seems to have any effect for me. Maybe Chrome changed. – Ryan – 2019-12-17T19:06:33.947

Omg cool! How do I install it? (Sorry I have no experience with developing Chrome extensions, haha.) – user541686 – 2011-05-16T09:05:20.603

@Mehrdad expanded the answer – Sathyajith Bhat – 2011-05-16T09:19:09.220

ofcourse, if you aren't looking at editing the files, I could've packed the files in to a Chrome Extension.... – Sathyajith Bhat – 2011-05-16T09:21:38.967

Beautiful, thank you so much! It teaches me some JavaScript too! :D – user541686 – 2011-05-16T09:25:59.533

welcome! I had developed this to learn Chrome extension development. (and to get around coughwebsensecough). – Sathyajith Bhat – 2011-05-16T11:26:11.687

Perfect. Exactly what I needed to get around the NYTimes paywall... – Yossi Farjoun – 2011-08-25T14:10:49.063

Thanks! BTW, is there a way to debug in chrome the script that is in bg.html? – Yossi Farjoun – 2011-08-25T14:36:01.783

@YossiFarjoun Yes, you log to error console using console.log

– Sathyajith Bhat – 2011-08-25T15:28:25.057

Note, I tried this and you don't actually need to use a bg.html for this plugin. You can just assign 'window.location = newurl' instead of doing the message passing. Maybe that wasn't true on older versions of Chrome? – apenwarr – 2012-02-16T00:42:10.280

@apenwarr You need to go the content script way to inject the code to analyse the page, and at least earlier, window.location wasn't possible. Refer to my [so] question How do I redirect to a URL using a Google Chrome Extension & Content Script?. window.location = newurl was my first approach while writing this, maybe something's changed since then

– Sathyajith Bhat – 2012-02-16T02:39:23.053

Perhaps this has changed recently. It worked for me in Chrome 16/17. – apenwarr – 2012-02-17T03:47:50.680

@apenwarr are you doing a simple redirect or are you checking for conditions (as in if you're on domain x & so on)? Can you post a pastebin of your code? – Sathyajith Bhat – 2012-02-17T04:01:22.717

12

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. (Even whole URL can be replaced)
  4. Add/Remove/Modify Headers in HTTP(s) Request and Response. You can setup Header Modification Rules only for specified URLs now.

Screenshots for more understanding:

  • List of Rules

List of Rules

  • List of Rule Types

List of Rule Types

  • New Redirect Rule

Creating a Redirect Rule

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

  • Setting custom headers (Done)
  • Switching User Agents
  • Setting parameters in request (Done) Use Redirect/Replace feature to accomplish this.

.. and a lot more.

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

sachinjain024

Posted 2011-05-16T07:18:26.563

Reputation: 333

Headers Modification is now present in Requestly. – sachinjain024 – 2015-01-08T05:07:49.070

1This is an awesome extension! I logged in just so I can give you a +1, hopefully more people find out about it :) – Adi Ulici – 2015-02-11T12:11:41.943

Regex Support is also available in Requestly – sachinjain024 – 2015-10-15T04:45:19.410

@sachinjain024 the link to github repo is dead as of now. – Andrei Kucharavy – 2017-12-19T08:55:19.710

9

I have developed a pre packaged user friendly redirector called Switcheroo if you're interested:

Setup custom redirect rules for any http request i.e pages, scripts, images etc. Uses a simple string replace to do this.

ranjez

Posted 2011-05-16T07:18:26.563

Reputation: 99

1Too bad Switcheroo doesn't support switching HTTP schemes (as indicated in the example in the question). – Martijn – 2014-07-06T19:02:14.653

2Also too bad it needs permission to "Read and change all your data on the websites you visit" just to redirect. – Taylan – 2017-02-01T20:21:52.910

4Open source? :) – user72923 – 2012-11-19T23:10:30.890

3

A bit late, but this extension should surely do the trick: Redirector.

And it's an arbitrary redirector.

user1237509

Posted 2011-05-16T07:18:26.563

Reputation: 31

Works great to avoid the Facebook start page, Match: http?://www.facebook.com, Substitute:(.*), Replacement:https://www.facebook.com/messages

– lony – 2014-04-25T09:51:19.603

3oh no, it's been removed? looked great from the screenshots! (regexes and such, which is what I need) – Don Hatch – 2014-05-22T00:38:57.237