-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to do 301 Redirects #37
Comments
node-http-proxy doesn't support 301 redirects yet, but I will leave this issue as this is something we want to address in future versions. |
Thanks for the quick answer. I guess I could put the redirect logic into the actual server that the domain is served on. I'll post my solution to that when I get it working. It will help those that have the same issue. |
For reference here's the solution I ended up using. Note: I'm using express so it's express specific code:
|
But what happens if i don't know the URL redirect? thanks |
Now http-proxy supports connect middleware you can use no-www var http = require('http'), var options = { var proxyServer = httpProxy.createServer( proxyServer.listen(80); |
I got this working, here's my code, inserted in the middle of the boilerplate http+websockets example: |
here's a generic one - modified from @francoislaberge example - I use as the first piece of middleware in my express app: express.use(function(req,res,next){
var host = req.get('host');
if(/^www\./.test(host)){
host = host.substring(4, host.length);
res.writeHead(301, {'Location':req.protocol + '://' + host + req.originalUrl,
'Expires': new Date().toGMTString()});
res.end();
} else {
next();
}
}); You could reverse it just as easily making sure that host always starts with |
Http-proxy doesn't still support 301 Redirects?? var url = require("url") I don't find very useful a proxy like this, sincerely. |
@q2dg i think you are mistaking terminology. Your code correctly proxies any request to the server to google.com. This is not the same as redirecting to google as the traffic is still going through your server. |
Well, maybe I haven't stated the problem incorrectly, sorry. If you try my above code (having configured proxy settings in a browser pointing to it) you will see it doesn't work: browser shows a "restarted connection" error. But if you change "www.google.com" by another URL which doesn't suffer from redirection (for instance, "www.linux.com"), it does work (withouth changing browser's direction bar, but it doesn't matter). At least, this is what happens in Spain, where "www.google.com" is automatically redirected to "www.google.es". It's the only explanation I have: http-proxy doesn't handle 3xx responses, like official http.get() method either. But, of course, I can be wrong. |
@q2dg ok well this does seem plausible. This is something that should be handled in some manner. If you can come up with a good failing test case (integrated into the actual tests) I can take a stab at implementing it |
Ok, I will try. Thanks!! |
+1 to add redirection support. We could have the following simple format to indicate whether the request should be redirected to proxies.
If the http-proxy notices the |
I think this is scope of a different project. In fact |
I have just implemented the redirection as follows without changing a code in http-proxy.
Then in the
Comments are welcome. |
This should technically be supported with https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/passes/web-outgoing.js#L49-L70. If not please open a new issue. |
How about support for |
I'm trying to remove the www. requests to point them to the canonical non www. urls. This is the make sure copy pasted urls point to one domain and thus not divide up my Google Page Rank juice across the two domains.
The text was updated successfully, but these errors were encountered: