Forum Moderators: phranque

Message Too Old, No Replies

Redirect non-existant urls from one domain to another

Looks like textbook stuff according to the apache docs but just won't work

         

rhino777

7:34 am on Feb 6, 2006 (gmt 0)

10+ Year Member



I'm trying to set things up so when a good url is requested from the server, it's served up as usual. When a bad url is requested I want to redirect from [server1...] to [server2...] I've already hit the apache module docs and this is almost a copy and paste.

A snip of my http.conf:

RewriteEngine on
RewriteLog logs/rewrite.log
RewriteLogLevel 9
RewriteCond %{REQUEST_URI}!-U
RewriteRule ^(.+) [server2...] [R,L]

Relevant rewrite.log bits:

10.122.21.147 - - [06/Feb/2006:01:14:23 --0500] [server1/sid#25c148][rid#5c8158/initial] (2) init rewrite engine with requested uri /api
10.122.21.147 - - [06/Feb/2006:01:14:23 --0500] [server1/sid#25c148][rid#5c8158/initial] (3) applying pattern '^(.+)' to uri '/api'
10.122.21.147 - - [06/Feb/2006:01:14:23 --0500] [server1/sid#25c148][rid#5c4060/subreq] (2) init rewrite engine with requested uri /api
10.122.21.147 - - [06/Feb/2006:01:14:23 --0500] [server1/sid#25c148][rid#5c4060/subreq] (1) pass through /api
10.122.21.147 - - [06/Feb/2006:01:14:23 --0500] [server1/sid#25c148][rid#5c8158/initial] (5) RewriteCond URI (-U) check: path=/api -> status=200
10.122.21.147 - - [06/Feb/2006:01:14:23 --0500] [server1/sid#25c148][rid#5c8158/initial] (4) RewriteCond: input='/api' pattern='!-U' => not-matched
10.122.21.147 - - [06/Feb/2006:01:14:23 --0500] [server1/sid#25c148][rid#5c8158/initial] (1) pass through /api

As you can see I get a 200 status OK for the api uri even though I end up getting a 404 back from apache when I surf there (it tries to still pull it up from server1). I've been googling around for a bit and have found one or two posts suggesting something to do with the way subrequests work but no real solution.

This is Apache 2.0.55 on Windows Server 2003 by the way.

milanmk

7:53 am on Feb 6, 2006 (gmt 0)

10+ Year Member



Put this in .htaccess at your Server1.

RewriteEngine on
RewriteCond /%{REQUEST_FILENAME}!-f
RewriteRule ^(.+) http://server2.com/$1

rhino777

9:55 pm on Feb 6, 2006 (gmt 0)

10+ Year Member



Ok, now it _always_ redirects instead of just for incorrect urls.

Also, I'm looking to redirect more like missing diretories than actual files.

Thanks for any help!

jdMorgan

11:45 pm on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For directories:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) http://server2.com/$1 [R=301,L]

Sometimes it's a bother to get this kind of rewrite working, because you can't 'see' the value of REQUEST_FILENAME to be sure it is resolving correctly. If you have trouble, it's often useful to use something like the following rule temporarily to see the value. Then, if you need the leading slash or you need to add %{DOCUMENT_ROOT} to the RewriteCond, you will be able to see that in the address bar of the browser:

RewriteEngine on
RewriteRule ^(.+) http://server2.com/$1?Filename=%{REQUEST_FILENAME} [R=301,L]

Jim

[edit] Clarified wording on temporary test rule. [/edit]

[edited by: jdMorgan at 2:56 am (utc) on Feb. 7, 2006]

milanmk

2:47 am on Feb 7, 2006 (gmt 0)

10+ Year Member



There is a space between /%{REQUEST_FILENAME} and !-f, change this and it should work fine for missing files.