Pergunta

We are building a mobile site whose url structure matches our current desktop site. We are hosting them on the same domain, and using apache to filter traffic between them using WURFL to help decide which agents go where.

Our filtering rules currently look like this:

RewriteCond %{HTTP_COOKIE} "bucket=mobile"
RewriteRule ^(.*)$ http://internal-mobile-pool.ourdomain.com/$1 [P]

RewriteRule ^(.*)$ http://internal-desktop-pool.ourdomain.com/$1 [P]

Our WURFL based solution sets the 'bucket' cookie for us.

The problem we are facing is that not all the urls on our desktop site have been re-implemented on our mobile site. What I would ideally like, is for the above rule to apply, but if it tries to go to the mobile servers, and gets a 404, it serves up the content from the desktop servers instead.

ie If a mobile requests www.ourdomain.com/some_desktop_only_resource.html, and the internal-mobile-pool.ourdomain.com/some_desktop_only_resource.html returns a 404 - for it to return the content on desktop-pool.ourdomain.com/some_desktop_only_resource.html

To describe this in pseudo-code

if(isMobile)
   response = getMobileResponse(url)
   if(response.code != 404)
        serveResponse(response)
   else
        serveResponse(getDesktopResponse(url))

I know this is possible if I list all the supported urls in this file - I would like to avoid this, as I would like this Platform Recognition layer to be independent of the application it serves. I also know I could solve this in the mobile application itself by redirecting from there, but if at all possible, I would like this Platform Recognition layer to be self-contained.

Is this possible using mod_rewrite?

Foi útil?

Solução

Don't think there's a way to do this using mod_rewrite. Because you're using the [P] flag to proxy, the RewriteRule hands the proxy request to mod_proxy and doesn't even know if it went through or not. If everything was in the same document root (or at least subdirectories of the document root), you'd be able to use the -f conditional check to see if a file exists.

Something you could try doing, though I have no idea if this will work, is using mod_proxy's ProxyErrorOverride along with ErrorDocument. If your ProxyErrorOverride is turned on, theoretically if a proxied request returns an error, mod_proxy will use the local error handler instead of the proxied one. You could then use a ErrorDocument 404 to handle the proxied 404 with a php script or something, then do something about re-routing it back through the local rewrite engine (with maybe a redirect with a special query string param?) so that it could be proxied to the desktop domain.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top