Using mod_rewrite to serve the mobile version of a page

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

Is it possible? I tried this:

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

... But it doesn't seem to work. In particular, the second RewriteCond does not match. I think I might need to give him an absolute path. But if so, how can I do this when I insert the ".mobile" part? And if not, what am I doing wrong?

+3
source share
1 answer

To check -fyou need the absolute path, as you thought. This doesn't seem to be a big issue 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]
+1
source

Source: https://habr.com/ru/post/1757895/


All Articles