I think what I’m trying to do is more complex than I originally thought. The first part is straight forward: I’m using .htaccess to redirect mobile browsers to the “mobile” verson of a website on a different domain. Here’s the contents of .htaccess in the root of the “non-mobile” site:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} android.+mobile|iphone [NC]
RewriteRule ^(.*)$ http://m.domain.tld%{REQUEST_URI} [R]
The second part adds some complexity. I’m using .htaccess on the mobile domain to redirect most requests to index.php in the root directory. Here’s the contents of .htaccess in the root of the mobile site:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
When a request that’s sent to the non-mobile site contains only the domain name, this all works as expected:
http://www.domain.tld
gets redirected to
http://m.domain.tld
with no problem.
Also, If a mobile browser loads
http://m.domain.tld/abc
it works as expected: index.php in the root of the mobile domain is loaded, and it parses the request URL, and assigns the value “abc” to a variable.
However, things break down strangely when I try to navigate to
http://www.domain.tld/abc
in a mobile browser. This results in the non-mobile file,
http://www.domain.tld/abc/index.php
being loaded into the browser, formatted by CSS from the mobile domain. The initial redirect that I want to be executed is not executed, but apparently, subsequent requests, like the one for the CSS file, are being redirected. Both domains are vhosts on the same server.
Can anyone weed through what I’m trying to do and offer a solution?
Thanks,
Dave
I figured this out. The problem was that there was no .htaccess in directory /abc on http://www.domain.tld to redirect mobile browsers. I guess I was expecting Apache to magically ignore the fact that the /abc directory exists and process all requests through the .htaccess in the root directory of the vhost.
So, the request for
http://www.domain.tld/abcwas not being redirected. And the “mobile formatting” was being applied because although the address bar showed “http://www…”, the request for that resource was coming from “http://m…”, and the CSS file exists on the mobile domain.