htaccess - 使用查询字符串将子域的请求重写到文件夹

发布于 2024-09-17 19:28:54 字数 526 浏览 4 评论 0原文

我正在尝试使用 Apache htaccess 文件进行子域重写,并且需要一些帮助。

我正在尝试获取对 http://xyz.example.com 的请求,内部处理就像用户请求http: //example.com/xyz 因此用户不会在地址栏中看到此 URL - 他们会看到子域版本。

这有点复杂,因为以下的任何内容也需要传递,例如,如果用户请求http://xyz.example.com /test/word/?x=123 那么这需要由服务器处理,就好像他们访问了http://example.com/xyz/test/word /?x=123

该网站在 Code Igniter 框架上运行,该框架也有自己的 htaccess 规则,但我想在这些规则生效之前将首先完成此重写。

任何帮助将不胜感激!

I'm trying to get a subdomain rewrite working using an Apache htaccess file and need some help please.

I am trying to get requests for http://xyz.example.com internally handled as if the user had requested http://example.com/xyz so the user does not see this URL in their address bar - they see the subdomain version.

This is slightly complicated by the fact that anything following this also needs to be passed too, for example if the user requests http://xyz.example.com/test/word/?x=123 then this needs to be handled by the server as if they had visited http://example.com/xyz/test/word/?x=123.

The site is running on the Code Igniter framework, which had its own htaccess rules too, but I guess this rewrite would be done first before those kick in.

Any help would be greatly appreciated!

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

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

发布评论

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

评论(1

那小子欠揍 2024-09-24 19:28:54

以下 mod_rewrite 规则集应执行您需要的重写:

RewriteCond $0 !^(index\.php/)?xyz
RewriteCond %{HTTP_HOST} ^xyz\.
RewriteRule ^.*$ /xyz/$0 [L]

查询字符串会自动传递,因为我们在重写中没有更改它。

不过,要让 CodeIgnitor 接受此更改,您需要使用 PATH_INFO 作为路由信息的来源。为此,您的 $config['uri_protocol'] 需要设置为 PATH_INFOAUTO,并且您的 CodeIgnitor RewriteRule 应如下所示:

RewriteRule ^.*$ /index.php/$0

如果您在尝试使用 PATH_INFO 时遇到问题,则需要找到一种方法让 CodeIgnitor 解析 $_SERVER['REDIRECT_URL '] (并将 L 标志添加到第一个 RewriteRule 中)。希望这不是必要的,但如果你不能让它工作,否则我会研究你如何做到这一点。

The following mod_rewrite rule set should perform the rewriting you need:

RewriteCond $0 !^(index\.php/)?xyz
RewriteCond %{HTTP_HOST} ^xyz\.
RewriteRule ^.*$ /xyz/$0 [L]

The query string gets passed along automatically, since we haven't changed it in the rewrite.

For CodeIgnitor to pick this change up though, you'll need to be using PATH_INFO as the source of the routing information. Your $config['uri_protocol'] needs to be set to PATH_INFO or AUTO for this to be the case, and your CodeIgnitor RewriteRule should look something like the following:

RewriteRule ^.*$ /index.php/$0

If you run into trouble trying to use PATH_INFO, you'll need to find a way to get CodeIgnitor to parse $_SERVER['REDIRECT_URL'] (and add the L flag to the first RewriteRule up there). Hopefully this shouldn't be necessary, but if you can't get it to work otherwise I'll look into how you might do that.

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