使用 mod_rewrite 处理非硬编码域名
我正在迁移我的应用程序,该应用程序为每个用户提供一个子站点,从domain.com/~user 到user.domain.com。为了做到这一点,我编写了以下 RewriteRule
:
RewriteRule ^~([a-z_]+)(/.*)?$ http://$1.%{HTTP_HOST}$2 [R=301,QSA,NC]
但是,%{HTTP_HOST}
并没有完全满足我的需要,因为如果用户浏览到 www.domain.com/~user
,它会重定向到 user.www.domain.com
,这显然不是我要找的。
我知道我可以将 %{HTTP_HOST}
替换为硬编码域,但我也不想这样做,因为我将在多个域上推出更改,并且不想必须为每一个进行定制。有没有更好的方法可以在不进行硬编码的情况下进行单一更改? (此外,如果基域已经有子域,即 sub.domain.com/~user
-> user.sub.domain.com
,该怎么办?
I am migrating my application which provides a subsite for each user from domain.com/~user to user.domain.com. In order to do this, I wrote the following RewriteRule
:
RewriteRule ^~([a-z_]+)(/.*)?$ http://$1.%{HTTP_HOST}$2 [R=301,QSA,NC]
However, %{HTTP_HOST}
doesn't do exactly what I need it to, because if for instance a user browses to www.domain.com/~user
, it'll redirect to user.www.domain.com
which is obviously not what I'm looking for.
I know that I can replace %{HTTP_HOST}
with a hardcoded domain, but I don't want to do this either, because I will be rolling out the changes on multiple domains and don't want to have to customize it for each one. Is there a better way to make a singular change without hardcoding? (Furthermore, what if the base domain already has a subdomain -- ie. sub.domain.com/~user
-> user.sub.domain.com
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用此附加
RewriteCond
:这将从主机中删除
www.
前缀(如果存在)。Try it with this additional
RewriteCond
:This will remove the
www.
prefix from the host if present.