使用mod_rewrite服务于网页的移动版本 [英] Using mod_rewrite to serve mobile version of a page

查看:131
本文介绍了使用mod_rewrite服务于网页的移动版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个可选的移动布局添加到现有的网站。在某些情况下,我希望能够成为完全不同内容的移动版本比在普通版相同的页面。如果用户访问,比方说,m.example.com/food/apple.html,他会看到一个完全不同的页面比example.com/food/apple.html,没有并购。子域。具体而言,我希望能够创建单独的apple.html和apple.mobile.html目录中的文件,然后使用mod_rewrite服务经常一个移动版本,而不是如果用户是在M。子域。然而,并不是每个页面都会有一个单独的移动版本,所以我需要先检查,以确保移动版本试图服务之前确实存在。

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]

...但它似乎并没有工作。具体地,第二的RewriteCond不匹配。我想我可能需要给它一个绝对路径。但是,如果是这样,我怎么能做到这一点,而插入。移动的一部分?如果不是,我是什么做错了吗?

…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?

推荐答案

对于 -f 检查,你确实需要一个绝对路径像你这样想。看起来这应该不是太大的问题,你的情况,你可以只使用%{DOCUMENT_ROOT}

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]

这篇关于使用mod_rewrite服务于网页的移动版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆