如果文件夹不存在,Apache .htaccess 重定向
我需要执行以下操作
我有一个网站,如果 http://example.com/user1 不存在, ,将用户重定向到 http://user1.example.com
类似地 http://www.example.com/user2 应重定向到 http://user2 .example.com
使用 .htaccess 文件可以实现这一点吗?
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://%{HTTP_HOST} [R]
上面重定向到主页,但是如何读取 / 后面的 URL 并进行上面的重定向呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
REQUEST_URI
上有点麻烦的正则表达式确保只有http://example.com/user1
方案的 URL 才会被重定向(而不是/user1/foo /栏
)。请注意使用
SERVER_NAME
而不是HTTP_HOST
(后者是邪恶的,应该避免)。The somewhat cumbersome regex on
REQUEST_URI
ensures, only URLs of thehttp://example.com/user1
scheme will be redirected (as opposed to/user1/foo/bar
).Note the use of
SERVER_NAME
instead ofHTTP_HOST
(the latter is evil and should be avoided).尝试将以下内容添加到 example.com 网站根目录中的
.htaccess
文件中。Try adding the following to the
.htaccess
file in the root directory of your example.com site.