我有以下代码:
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时,它会重定向到http://domain.com/ipad/shop-name/这不应该发生,没有人应该看到ipad目录。
有谁知道如何解决这个问题?
谢谢!
答案 0 :(得分:1)
这可能是因为mod_dir正在内部处理请求。当您访问http://m.domain.com/shop-name然后将其重写为http://domain.com/ipad/shop-name时,mod_dir 302会将浏览器重定向到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重定向