Forum Moderators: phranque

Message Too Old, No Replies

URL Rewriting, looping, proxying question/problem

         

rugbyhead

9:37 am on Nov 3, 2008 (gmt 0)

10+ Year Member



Heyall,
trying to conjure up the following situation on a web server for a project...

1. user visits "domain.com", the url stays at "domain.com" but the content displayed is from "domain.com/path/file.ext"

2. user visits "domain.com/path/file.ext", the url redirects to "domain.com" but the content displayed is from "domain.com/path/file.ext"

Can achieve the first one fine by using a:
RewriteRule ^/$ /path/file.ext [P]

but combining both things is proving very tricky, and keep getting redirect loops, despite trying things with S, N, C, L flags....
Probably just getting the exact combination slightly wrong, but it's driving me crazy and am calling out for your help!

TIA

jdMorgan

1:38 pm on Nov 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that HTTP is a stateless protocol, and therefore the server itself has no memory of any previous request or response. Therefore, if a redirect [R=30x] or proxy throughput [P] is done, invoking a new HTTP transaction, then all of your redirect and rewrite code will be re-processed. This leads to loops such as you describe.

The solution is that your "no direct access to hidden URL" redirect rule must use a RewriteCond to examine the variable %{THE_REQUEST} to be sure that the URL is the "hidden" one because the client directly-requested it, before you redirect. Otherwise, the "hidden" URL is being internally requested because it has already been rewritten by your other rule, and it must not be redirected or a loop will result as the two rules repeatedly countermand each other.

Using a proxy throughput should not be required; If both paths are in the same domain, you need only the canonical URL redirect for your second case, followed by the an internal rewrite for your first case.

The second case is very similar to the "Redirect 'index.html' to '/' redirects we discuss here often.

Jim