使用 mod_rewrite 提供页面的移动版本

发布于 2024-09-12 12:28:56 字数 560 浏览 1 评论 0原文

我正在尝试向现有网站添加可选的移动布局。在某些情况下,我希望能够为移动版本提供与常规版本相同页面完全不同的内容。例如,如果用户访问 m.example.com/food/apple.html,他将看到与 example.com/food/apple.html 完全不同的页面,没有 m。子域。具体来说,我希望能够在目录中创建单独的 apple.html 和 apple.mobile.html 文件,然后使用 mod_rewrite 来提供移动版本而不是常规版本(如果用户位于 m 中)。子域。但是,并非每个页面都有单独的移动版本,因此我需要首先检查以确保移动版本确实存在,然后再尝试提供服务。

这可能吗?我尝试过这个:

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

……但似乎不起作用。具体来说,第二个 RewriteCond 不匹配。我想我可能需要给它一个绝对路径。但如果是这样,我该如何在插入“.mobile”部分时做到这一点?如果不是,我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦冥 2024-09-19 12:28:58

对于 -f 检查,您确实需要像您想象的那样的绝对路径。对于您的情况来说,这似乎不是什么大问题,因为您可以使用 %{DOCUMENT_ROOT}

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

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文