.htaccess 重写规则条件

发布于 2024-12-14 09:46:18 字数 639 浏览 0 评论 0原文

我有以下代码:

RewriteCond %{HTTP_HOST} ^m\.domain\.com?$
RewriteRule ^ http://domain.com/ipad%{REQUEST_URI} [L,P]

所以我的子域 http://m.domain.com 确实是从 http://domain.com/ipad/ 中提取的,

一切正常如果我输入: http://m.domain.com/shop-name/ 它工作完美。但是当我省略尾部斜杠并输入 http://m.domain.com/shop-name< /a> 它重定向到 http://domain.com/ipad/shop-name/这是不应该发生的,没有人应该看到 ipad 目录。

有谁知道我该如何解决这个问题?

谢谢你!

I have the following code:

RewriteCond %{HTTP_HOST} ^m\.domain\.com?$
RewriteRule ^ http://domain.com/ipad%{REQUEST_URI} [L,P]

So my subdomain, http://m.domain.com is really pulling from http://domain.com/ipad/

Everything works fine if I type in: http://m.domain.com/shop-name/ it works perfectly. But when I leave out the trailing slash and type in http://m.domain.com/shop-name it redirects to http://domain.com/ipad/shop-name/ which shouldn't happen, no one should see the ipad directory.

Does anyone know how I can fix this?

Thank you!

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

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

发布评论

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

评论(1

暮光沉寂 2024-12-21 09:46:18

这可能是因为 mod_dir 正在内部处理请求。当您访问 http://m.domain.com/shop-name 时,它会被重写到 http://domain.com/ipad/shop-name,mod_dir 302 将浏览器重定向到<一href="http://domain.com/ipad/shop-name/" rel="nofollow">http://domain.com/ipad/shop-name/。

您可以尝试处理 RewriteRule 中的尾部斜杠。可能是一种更简洁的方法,但大致如下:

RewriteCond %{HTTP_HOST} ^m\.domain\.com?$
# Has trailing slash in request, don't need to append one in the RewriteRule
RewriteCond %{THE_REQUEST} ./\ HTTP/1\.[01]$
RewriteRule ^ http://domain.com/ipad%{REQUEST_URI} [L,P]

RewriteCond %{HTTP_HOST} ^m\.domain\.com?$
# Missing trailing slash in request
RewriteCond %{THE_REQUEST} [^/]\ HTTP/1\.[01]$
# Request doesn't end with one of these extensions, 301 redirect to include trailing slash
RewriteCond %{REQUEST_FILENAME} !\.(php|html?|jpg|gif)$
RewriteRule . http://m.domain.com%{REQUEST_URI}/ [R=301]

编辑:编辑以解决缺少尾随斜杠的 301 重定向

This might be because mod_dir is processing the request internally. When you access http://m.domain.com/shop-name and then it gets rewritten to http://domain.com/ipad/shop-name, mod_dir 302 redirects the browser to http://domain.com/ipad/shop-name/.

You can try handling the trailing slash in your RewriteRule. Probably a cleaner way of doing this, but something along the lines of:

RewriteCond %{HTTP_HOST} ^m\.domain\.com?$
# Has trailing slash in request, don't need to append one in the RewriteRule
RewriteCond %{THE_REQUEST} ./\ HTTP/1\.[01]$
RewriteRule ^ http://domain.com/ipad%{REQUEST_URI} [L,P]

RewriteCond %{HTTP_HOST} ^m\.domain\.com?$
# Missing trailing slash in request
RewriteCond %{THE_REQUEST} [^/]\ HTTP/1\.[01]$
# Request doesn't end with one of these extensions, 301 redirect to include trailing slash
RewriteCond %{REQUEST_FILENAME} !\.(php|html?|jpg|gif)$
RewriteRule . http://m.domain.com%{REQUEST_URI}/ [R=301]

EDIT: edited to address the 301 redirect of missing trailing slash

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