Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Community > MacNN Lounge > Apache Question

Apache Question
Thread Tools
mitchell_pgh
Posting Junkie
Join Date: Feb 2000
Location: Washington, DC
Status: Offline
Reply With Quote
Sep 9, 2009, 09:51 PM
 
I work for a university and could use some advice from others that are in the know about Apache.

I'm looking to point a directory to a different server (Example: I want everything in http://www.mysite.com/blog/ to go to another server... but it should look seamless from the outside world)

I'm being told that it can't be done because we are already rerouting Apache and it may break Apache. They also indicated that if the other server went down, it could cause serious problems.

Thoughts?
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 9, 2009, 10:22 PM
 
You need to think about this more at the DNS level than the Apache level. DNS entries can only either resolve to IP, or to several if you wish to setup a DNS round robin pool. You cannot have some requests to a certain directory in an HTTP request resolve to one IP, and another IP for other HTTP requests. DNS doesn't know anything about HTTP, Apache, or any other service you might want to serve via that domain. This is not an Apache problem or an Apache question, it's a DNS one.

Options for you:

- Redirect to a subdomain hosted on this other server
- Sync the data between the servers (several ways to do that)
- Fetch data from this server in real time (several ways to do this too)
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 9, 2009, 10:43 PM
 
Couldn't you proxy to the other server?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 9, 2009, 10:52 PM
 
If you are asking me, I don't know, I've never spent time with setting up proxies. I used Squid years ago though, but only for bandwidth throttling. Whether you could proxy only for certain directories is another unknown. I think as far as the original poster is concerned, there are probably easier solutions (such as the subdomain).
     
Doofy
Clinically Insane
Join Date: Jul 2005
Location: Vacation.
Status: Offline
Reply With Quote
Sep 9, 2009, 10:54 PM
 
IIRC, Squid will do it (reverse proxy setup, obviously). But I agree with Bess on the subdomain thing.
Been inclined to wander... off the beaten track.
That's where there's thunder... and the wind shouts back.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 9, 2009, 10:54 PM
 
Yeah, a subdomain would be way easier, but I'm pretty sure you can accomplish what Mitchell's looking for (though my Apache-fu is quite rusty and I couldn't tell you exactly how to do it offhand).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 9, 2009, 11:07 PM
 
Setting up Squid would require root access to the server too, I'm sure.

I'd suggest learning and exploring as much as you can before diving in to this. If there is anything fuzzy you should clear that up for your own benefit, as well as to explore easier options. Using a proxy like this, AFAIK, would be a pretty strange and conventional way to go about solving this that I'd probably consider a last resort. Squid is not going to be easy if you are new to all of this.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 9, 2009, 11:08 PM
 
Another option, a very ugly and lazy man's option but one that would work in a pinch: an iframe.
     
mduell
Posting Junkie
Join Date: Oct 2005
Location: Houston, TX
Status: Offline
Reply With Quote
Sep 10, 2009, 02:56 PM
 
Why not just use an HTTP 301/302 as appropriate?

Or mod_rewrite if you want to preserve the URL.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 10, 2009, 03:02 PM
 
The only way you can use mod_rewrite to change a URL from one server to a URL from another is with a redirect — so basically your option 1 is the only choice.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 10, 2009, 03:04 PM
 
Originally Posted by mduell View Post
Why not just use an HTTP 301/302 as appropriate?

Or mod_rewrite if you want to preserve the URL.
He would need a URL to redirect to, and mod_rewrite does not rewrite external URLs, only local paths.
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 10, 2009, 03:14 PM
 
Use mod_rewrite

If not, do what I do using PHP


<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 10, 2009, 03:51 PM
 
Originally Posted by hyteckit View Post
Use mod_rewrite
No go for external URLs, as has been said

If not, do what I do using PHP


<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
This is just a screen scraper, you don't get the Javascript DOM manipulation execution, Javascript event handlers, etc. Images that aren't referenced within an absolute URL including the domain will be broken, and all external CSS rules do not come along for the ride.
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 10, 2009, 09:33 PM
 
Originally Posted by besson3c View Post
No go for external URLs, as has been said



This is just a screen scraper, you don't get the Javascript DOM manipulation execution, Javascript event handlers, etc. Images that aren't referenced within an absolute URL including the domain will be broken, and all external CSS rules do not come along for the ride.
Not true.

I have 1 php application/website running multiple domains.

Just need to use mod_rewrite and redirect all paths to the main php script.

If the javascript, css, and images are relative paths, they should work fine just by setting the:

<base href="http:www.newsite.com">


If the links are absolution paths, you'll need to do a global replace of the old url with the new url.
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 10, 2009, 09:37 PM
 
Could you show us the rewrite rules? I'd like to see them, if I'm wrong this would be handy to learn.
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 10, 2009, 09:55 PM
 
.htaccess
----------
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/blog/ [NC]
RewriteRule ^blog/(.*)$ blog.php?$1 [L]


Every request that contains the URI ^/blog/ would get redirected to the script blog.php.

blog.php would be the script that contains:

<?php
$query_string = explode('/',$_SERVER["REQUEST_URI"]);
#Do what you need to do with $_SERVER["REQUEST_URI"]
$homepage = file_get_contents('http://www.example.com/'); # or whatever url that was requested
#replace the base href with the new url. replace the absolute paths with the new url
echo $homepage;
?>
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 10, 2009, 09:58 PM
 
Okay, but what about all of the other files that www.example.com depends on? How are these local requests mapped to the external domain so that all of the external Javascript, CSS, image, etc. files work?
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 10, 2009, 10:03 PM
 
Originally Posted by besson3c View Post
Okay, but what about all of the other files that www.example.com depends on? How are these local requests mapped to the external domain so that all of the external Javascript, CSS, image, etc. files work?
It does. Just do this:

<?php
$homepage = file_get_contents('http://www.newsite.com/'.$_SERVER["REQUEST_URI"]);
#replace the base href with the new url. replace the absolute paths with the new url
echo $homepage;
?>


I do this with microsoft's website and drop an image of some poop on top of it.
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 10, 2009, 10:09 PM
 
#replace the base href with the new url. replace the absolute paths with the new url
I was asking about how you do this, thinking that you wouldn't have to update the base hrefs in your application and HTML output... I guess you do though.

If that application would ever be relocated to a different domain again, I suppose you could generalize this by creating a dummy domain, putting this entry into your /etc/hosts, and just calling this domain from your app:

src="http://dummydomain.com/path/to/file"
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 10, 2009, 10:09 PM
 
Or if you are familiar with PERL, you can write the script using PERL and the awesome module LWP.

Perl & LWP - Google Books
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 10, 2009, 10:21 PM
 
Originally Posted by besson3c View Post
I was asking about how you do this, thinking that you wouldn't have to update the base hrefs in your application and HTML output... I guess you do though.

If that application would ever be relocated to a different domain again, I suppose you could generalize this by creating a dummy domain, putting this entry into your /etc/hosts, and just calling this domain from your app:

src="http://dummydomain.com/path/to/file"
Not sure what you mean.

Say you move your whole 'blog' site from http://www.oldsite.com/blog to a new website http://www.newsite.com/blog/.


Say if your images still contain the oldsite url:

<img src="http://www.oldsite.com/blog/sexyimage.jpg">

You would want to replace the url www.oldsite.com with www.newsite.com.


In your blog.php script:

$homepage = file_get_contents('http://www.newsite.com/'.$_SERVER["REQUEST_URI"]);
$homepage = preg_replace('/oldsite\.com/gi','newsite.com',$homepage);
echo $homepage;
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 10, 2009, 10:29 PM
 
I guess you script is now a proxy script.

It handles all the request.
Processes the request and fetches the data from the desire location.
Parse out the data and rewriting all its paths.
Then prints out the data.

If you are geeky enough, you can cache the results according to the requested URL, so you don't have to keep fetching from the new site. Even if the new site is down, you'll have a cache version.
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 11, 2009, 12:42 AM
 
Originally Posted by hyteckit View Post
Not sure what you mean.

Say you move your whole 'blog' site from http://www.oldsite.com/blog to a new website http://www.newsite.com/blog/.


Say if your images still contain the oldsite url:

<img src="http://www.oldsite.com/blog/sexyimage.jpg">

You would want to replace the url www.oldsite.com with www.newsite.com.


In your blog.php script:

$homepage = file_get_contents('http://www.newsite.com/'.$_SERVER["REQUEST_URI"]);
$homepage = preg_replace('/oldsite\.com/gi','newsite.com',$homepage);
echo $homepage;

What about paths in external CSS files or Javascript files? You'd have to open these and parse them, and somehow create a local copy of these or something. What about paths that don't have a domain attached to them? You'd have to detect absolute and relative paths and manipulate these to become the proper absolute URL.

This is an interesting hack, but it would require a fair amount of parsing and manipulation of the pages it pulls in in order to work properly. I suppose it's a good last resort sort of technique, but I would say that this should only be an attractive option if you are fairly desperate to make this sort of thing work.

Of course, maybe it works beautifully for you if you do not use CSS background images referenced in an external file and stuff like that, or you don't mind reattaching these via a Javascript trigger on load or something like that.
     
hyteckit
Addicted to MacNN
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 11, 2009, 01:22 AM
 
Originally Posted by besson3c View Post
What about paths in external CSS files or Javascript files? You'd have to open these and parse them, and somehow create a local copy of these or something. What about paths that don't have a domain attached to them? You'd have to detect absolute and relative paths and manipulate these to become the proper absolute URL.

This is an interesting hack, but it would require a fair amount of parsing and manipulation of the pages it pulls in in order to work properly. I suppose it's a good last resort sort of technique, but I would say that this should only be an attractive option if you are fairly desperate to make this sort of thing work.

Of course, maybe it works beautifully for you if you do not use CSS background images referenced in an external file and stuff like that, or you don't mind reattaching these via a Javascript trigger on load or something like that.
If it's too much trouble then just use mod_rewrite for all the CSS, JS, and images. No one is really going to check where the CSS, JS, and images come from.

So just use mod_rewrite for all the JS, CSS, and images instead of using the PHP for rewrite.

.htaccess
----------
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/blog/ [NC]
RewriteRule ^blog/(.*)\.(css|js|ico|jpg|jpeg|png|gif|ico)$ http://www.newsite.com/blog/$1.$2 [L]
RewriteRule ^blog/(.*)$ blog.php?$1 [L]
Bush Tax Cuts == Job Killer
June 2001: 132,047,000 employed
June 2003: 129,839,000 employed
2.21 million jobs were LOST after 2 years of Bush Tax Cuts.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 11, 2009, 02:53 AM
 
Interesting. For some reason I thought that external rewriting wasn't permitted, but I see that it is here given this example:

^/somepath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo
via external redirection
From this page

I can see how all of this put together would work beautifully, the above being a key ingredient.

Rewriting is extremely useful, I actually use it all the time. It's reason enough to learn regex, but just when you think you know it all you learn something new. Cool

Thanks for bearing with me, hyteckit.
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 11, 2009, 02:57 AM
 
hyteckit: it looks like you should include an "[R=301]" or "[R]" flag at the end of the external redirection line, no?
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 03:33 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,