mod_rewrite复杂度问题
我需要根据子域来修改我的域...有两种情况我需要这样做:
我的网站位于不同的城市...所以 losangeles.example.com 应该本地化到洛杉矶等...本质上重写为 www.example.com/?loc=losangeles
我希望允许用户创建 username.example.com 以便轻松地将他们的个人资料传递给朋友...本质上重写为 www.example.com/ user.php?id=username
随着我的网站规模不断扩大,我计划拥有超过 300 个位置和数千个用户……这意味着手动编码每个重写规则会有点乏味。
我如何告诉 mod-rewrite 在位置子域和用户名子域之间进行解密?
I have a need to mod-rewrite my domain based off of subdomains...there are two scenarios in which i would need to do this:
my site is located in different cities...so losangeles.example.com should localized to los angeles, etc...essentially rewritten to www.example.com/?loc=losangeles
i want to allow users to create username.example.com to pass their profiles to friends with ease...essentially rewritten to www.example.com/user.php?id=username
As my site scales, I plan on having over 300 locations, and several thousand users...which means that hand coding each rewrite rule would be a little tedious.
How can I tell mod-rewrite to decipher between a location subdomain and a username subdomain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为从根本上来说,您需要重新考虑该策略,因为从长远来看,混合命名空间会引起很多麻烦。考虑一下如果您错过了城市名称(例如 alexandra.example.com)但用户已注册该用户名,会发生什么情况?此外,您最终将不得不执行一些操作,例如在尝试创建用户名时检查城市是否存在。
如果您确实想坚持这样做,一种可能性是将请求发送到一个点,然后该点对您的两个数据库进行查找,然后在内部设置 $city 或 $user 变量。
一旦您设置了 DNS 通配符,请在 Apache 中设置:,
然后正常设置虚拟主机的其余部分。
然后在应用程序中只需检查 $_SERVER['SERVER_NAME'],例如,
但随后您实际上必须有两个查询和附加逻辑来分离两个命名空间或处理冲突情况。
我会使用这样的命名空间
,这样可以避免出现问题,并且还意味着您可以根据您输入的城市自定义用户主页上可见的内容(如果需要)。
I think fundamentally you need to rethink that strategy as mixing your namespaces is going to cause a lot of headaches in the long term. Consider what happens if you have missed out a city name, such as alexandra.example.com but a user has registered that username? Also, you are going to end up with having to do things like checking whether a city exists at the point that a username is trying to be created.
If you really do want to stick with this, one possibility is simply to send the requests to a single point which then does the lookup against your two databases and then internally sets a variable of either $city or $user .
Once you have set up your DNS wildcarding, in Apache set:
and then the rest of your virtualHost as normal.
Then in the application just check against $_SERVER['SERVER_NAME'], e.g.
but then you're actually going to have to have two queries and additional logic to pull apart the two namespaces or handle clash situations.
I would use a namespace like
which will avoid the problems, and also mean that you can customise what is visible on the user's homepage depending on which city you've entered, if that is desirable.