htaccess - 使用查询字符串将子域的请求重写到文件夹
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下 mod_rewrite 规则集应执行您需要的重写:
查询字符串会自动传递,因为我们在重写中没有更改它。
不过,要让 CodeIgnitor 接受此更改,您需要使用
PATH_INFO
作为路由信息的来源。为此,您的$config['uri_protocol']
需要设置为PATH_INFO
或AUTO
,并且您的 CodeIgnitorRewriteRule
应如下所示:如果您在尝试使用
PATH_INFO
时遇到问题,则需要找到一种方法让 CodeIgnitor 解析$_SERVER['REDIRECT_URL ']
(并将L
标志添加到第一个RewriteRule
中)。希望这不是必要的,但如果你不能让它工作,否则我会研究你如何做到这一点。The following mod_rewrite rule set should perform the rewriting you need:
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 toPATH_INFO
orAUTO
for this to be the case, and your CodeIgnitorRewriteRule
should look something like the following: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 theL
flag to the firstRewriteRule
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.