mod_rewrite复杂度问题

发布于 2024-08-06 15:26:55 字数 380 浏览 2 评论 0原文

我需要根据子域来修改我的域...有两种情况我需要这样做:

  1. 我的网站位于不同的城市...所以 losangeles.example.com 应该本地化到洛杉矶等...本质上重写为 www.example.com/?loc=losangeles

  2. 我希望允许用户创建 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:

  1. 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

  2. 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 技术交流群。

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

发布评论

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

评论(1

傲影 2024-08-13 15:26:55

我认为从根本上来说,您需要重新考虑该策略,因为从长远来看,混合命名空间会引起很多麻烦。考虑一下如果您错过了城市名称(例如 alexandra.example.com)但用户已注册该用户名,会发生什么情况?此外,您最终将不得不执行一些操作,例如在尝试创建用户名时检查城市是否存在。

如果您确实想坚持这样做,一种可能性是将请求发送到一个点,然后该点对您的两个数据库进行查找,然后在内部设置 $city 或 $user 变量。

一旦您设置了 DNS 通配符,请在 Apache 中设置:,

ServerAlias *.example.com

然后正常设置虚拟主机的其余部分。

然后在应用程序中只需检查 $_SERVER['SERVER_NAME'],例如,

$server_name = addslashes($_SERVER['SERVER_NAME']);    
$query = "select * from instances where '{$server_name}' regexp replace(concat(server, '

但随后您实际上必须有两个查询和附加逻辑来分离两个命名空间或处理冲突情况。

我会使用这样的命名空间

<cityname>.example.com/user/<username>

,这样可以避免出现问题,并且还意味着您可以根据您输入的城市自定义用户主页上可见的内容(如果需要)。

),'.','\.') limit 1";

但随后您实际上必须有两个查询和附加逻辑来分离两个命名空间或处理冲突情况。

我会使用这样的命名空间

,这样可以避免出现问题,并且还意味着您可以根据您输入的城市自定义用户主页上可见的内容(如果需要)。

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:

ServerAlias *.example.com

and then the rest of your virtualHost as normal.

Then in the application just check against $_SERVER['SERVER_NAME'], e.g.

$server_name = addslashes($_SERVER['SERVER_NAME']);    
$query = "select * from instances where '{$server_name}' regexp replace(concat(server, '

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

<cityname>.example.com/user/<username>

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.

),'.','\.') limit 1";

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.

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