views:

30

answers:

1

I'm trying to add an optional mobile layout to an existing website. Under certain circumstances, I'd like to be able to serve entirely different content for the mobile version than for the same page on the regular version. If the user visits, say, m.example.com/food/apple.html, he'll see an entirely different page than for example.com/food/apple.html, without the m. subdomain. Specifically, I'd like to be able to create separate apple.html and apple.mobile.html files in the directory, then use mod_rewrite to serve the mobile version instead of the regular one if the user is in the m. subdomain. However, not every page will have a separate mobile version, so I'd need to first check to ensure that a mobile version actually exists before trying to serve it.

Is this possible? I've tried this:

RewriteCond %{SERVER_NAME} ^m\.
RewriteCond $1.mobile.html -f
RewriteRule (.*)\.html $1.mobile.html [L]

…But it doesn't seem to work. Specifically, the second RewriteCond isn't matching. I think I might need to give it an absolute path. But if so, how can I do it while inserting the ".mobile" part? And if not, what am I doing wrong?

+1  A: 

For the -f check, you do need an absolute path like you thought. It seems like this shouldn't be much of a problem in your case, as you can just use %{DOCUMENT_ROOT}:

RewriteCond %{SERVER_NAME} ^m\.
RewriteCond %{DOCUMENT_ROOT}/$1.mobile.html -f
RewriteRule (.*)\.html $1.mobile.html [L]
Tim Stone