子域中的子页面

发布于 2024-10-31 07:09:04 字数 1037 浏览 2 评论 0原文

好的,目的是为我们的会员动态创建无限数量的子域。 因此,对 www.example.com/sub_index.php?user=demo 的请求将转换为 demo.example.com url,而 www.example.com 由 index.php 提供服务。

我已启用名称服务器和 Web 服务器来接受通配符请求(实际上我联系我的托管服务器来执行此操作)。

在 htaccess 部分,我有以下内容:

####rewrite example.com into www.example.com
RewriteCond %{HTTP_HOST} ^example.com 
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/ 
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L] 

####subdomains 
RewriteCond %{HTTP_HOST} !^www\.example.com 
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com 
RewriteRule ^(.*)$ sub_index.php?user=%1 

因此,对 demo.example.com/ 和 demo.example.com/index.php 的请求由 www.example.com/sub_index.php?user=demo 提供

我的问题是我找不到让子页面在子域中工作的方法 例如:demo.example.com/somepage.php 应该由 www.example.com/sub_page.php?user=demo 提供 而 demo.example.com/otherpage.php?id=5 应由 www.example.com/sub_page2.php?user=demo&id=5 提供服务。

感谢您的任何建议/提示

Ok, purpose is to have unlimited number of subdomains, created dynamically for our members.
So, a request to www.example.com/sub_index.php?user=demo will translate to demo.example.com url, while www.example.com is served by index.php.

I have enabled name server and web server to accept wildcard requests (actually i contact my hosting server to do that).

On the htaccess part i have the following:

####rewrite example.com into www.example.com
RewriteCond %{HTTP_HOST} ^example.com 
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/ 
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L] 

####subdomains 
RewriteCond %{HTTP_HOST} !^www\.example.com 
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com 
RewriteRule ^(.*)$ sub_index.php?user=%1 

So, a request to demo.example.com/ and to demo.example.com/index.php is served by the www.example.com/sub_index.php?user=demo

My problem is that i cant find a way to have subpages work in the subdomain
For example: demo.example.com/somepage.php should be served by www.example.com/sub_page.php?user=demo
while demo.example.com/otherpage.php?id=5 should be servered by www.example.com/sub_page2.php?user=demo&id=5.

Thank you for any advice/tip

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

允世 2024-11-07 07:09:04

为了保持重写规则易于管理,最好的办法是转换网址而不引入新变量。

在您的示例中,不可能使用简单的文本替换从 somepage.php 到 sub_page.php,然后从 otherpage.php 到 sub_page2.php - 您将需要为每个页面一个新规则,因为只有您知道映射。将结果 url 基于源 url 中的数据会更容易。

例如,您可以设计一个方案,其中:

demo.example.com/somepage.php?start=5

转换为

www.example.com/sub_somepage.php?user=demo&start=5

使用此方案,可以从原始脚本的名称派生出目标脚本的名称。此方案的 RewriteRule 如下所示:

RewriteRule ^/([A-Za-z0-9_]+\.php) /sub_$1?user=%1 [qsappend]

让重写规则正常工作可能非常棘手。当您完善规则时,您可能希望将其添加到您的配置中:

RewriteLog "/tmp/httpd-rewrite.log"
RewriteLogLevel 5

然后您可以检查您的规则是否正在执行您想要的操作。有关这些指令如何工作的更多信息,请访问 httpd.apache.org。特别是,如果您想重定向到不同的主机来提供内容,则必须将新主机名添加到替换和 [redirect] 标志中。

To keep your rewriting rules manageable, you're best bet is to transform urls without introducing new variables.

With your example, it is impossible to use a simple textual replacement to get from somepage.php to sub_page.php and then otherpage.php to sub_page2.php - you will need a new rule for each page, because only you know about the mapping. Its a lot easier of you base the result url on data in the source url.

For example, you could devise a scheme whereby:

demo.example.com/somepage.php?start=5

transforms into

www.example.com/sub_somepage.php?user=demo&start=5

With this scheme, the name of the target script can be derived from the name of the original script. The RewriteRule for this scheme would look like:

RewriteRule ^/([A-Za-z0-9_]+\.php) /sub_$1?user=%1 [qsappend]

Getting rewrite rules to work correctly can be quite tricky. While you refine your rules, you will probably want to add this to your config:

RewriteLog "/tmp/httpd-rewrite.log"
RewriteLogLevel 5

You can then check that your rules are doing what you want. More information on how these directives work is at httpd.apache.org. In particular, if you want to redirect to a different host to serve content, you'll have to add the new host name to the substitution and the [redirect] flag.

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