Jump to content

.htaccess help (redirect to another server if page does not exist)


renobird
 Share

Recommended Posts

I have 2 sites running in different infrastructures.

site.com (LAMP / Processwire)

legacy.site.com (IIS server, no CMS)

Looking for a way to check legacy.site.com for a file/page if it's not found on site.com.

If it isn't found on legacy.site.com then return a 404 from there.

Example request:

site.com/sharks/index.aspx 

That page doesn't exist on site.com, so I need .htaccess to redirect the request over to legacy.site.com and check there.

If it doesn't exist there, then return a 404.

Not sure this is possible. My Rewrite skills are rusty.

Any help would be appreciated.

Link to comment
Share on other sites

Someone else might have a better solution, but the options I can think of right now would be using a "dynamic" RewriteMap -- or a static one, if it's possible to list all the URLs available at the legacy site. On the other hand, if you end up with a dynamic RewriteMap, I'd probably go with a simple module that hooks into page not found and checks there if the requested URL exists, perhaps combined with some sort of internal caching :)

Either way, using this kind of approach you will need to perform an additional query behind the scenes to see if the URL exists. While that's doable with -U in mod_rewrite too, it doesn't handle 404's (or, rather, it considers them "existing URLs") so it's not really useful here AFAIK.

  • Like 2
Link to comment
Share on other sites

Thank teppo.

What I'm doing now is taking any request that results in a 404 within Processwire and redirecting it to legacy.site.com

My 404 template

$session->redirect("http://legacy.site.com{$_SERVER['REQUEST_URI']}");

Netcarver sent me a message similar to what you mentioned (I think). Check if the page exists on the other server. If it does use fopen() to serve the page. If not, serve the 404 code. Well, that's what I understood from his message anyway. ;)

Link to comment
Share on other sites

Thanks Steve.

I needed to use this on an install that was pre getHttpCode() - no worries though php's get_headers() works fine for what I needed.

I only want to redirect if the status is 200 (there are a zillion old redirects that could cause loops).

$request = "http://legacy.site.com{$_SERVER['REQUEST_URI']}";
$response = get_headers($request);
$status = $response[0];
if(strpos($status,"200")){
    $session->redirect($request);
} 
I modded the Redirects module this afternoon to support all this. Might be able to get a PR in before the end of the day.

Keep an eye out over there.

:)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...