在 CI 应用程序中重定向到移动子域
我有一个 Web 应用程序,我想使用 jQuery Mobile 创建一个移动版本。现有应用程序是在 CodeIgniter 中构建的;我将尽可能使用相同的控制器、模型; (尤其是模型,因为无论如何我都需要相同的数据,可能必须编写新的控制器)。
我对如何开始有点困惑。但是,我想将我的移动版本放在子域 (m.myhost.tld
) 上,因为我的应用程序位于 www.myhost.tld
而我不这样做感觉想将其全部复制到另一个文件夹并保留两个,我有点困惑。
我知道我可以使用 CodeIgniter 中的用户代理库来检测移动浏览器并相应地加载视图;我只是不知道如何让它与子域一起工作。我是否需要在此处自定义我的 app/config/routes.php
文件,或者我可以使用一些 .htaccess
魔法来修复此问题吗?不过,我对 .htaccess
几乎没有任何经验。我唯一知道的是如何从 CI 应用程序中删除我的 index.php,这是一个 copypasta 片段。
编辑:我想知道我是否可以使用像 这样的教程< /a> 做我想做的事?它似乎在做或多或少相同的事情,只是使用动态用户名而不是简单的“m”。
编辑2:我想还有更多信息。
假设我使用 CodeIgniter 附带的用户代理库检测移动浏览器。我想将这些浏览器定向到 m.myhost.tld
。但是,我想要在移动网站上显示的内容来自一个名为 mobile
的控制器,我也可以通过 www.myhost.tld/mobile/
访问该控制器;所以我的问题是是否有一种方法可以将 URL 例如 www.myhost.tld/mobile/about
路由到 m.myhost.tld/about
。我什至不确定这是否可能,teehee。还在学习!
如果您能给我任何建议,我将不胜感激。多谢!
I have a web app of which I would like to create a mobile version with jQuery Mobile. The existing application is built in CodeIgniter; I'll be using the same controllers, models where I can; (especially models since I'll be needing the same data anyway, might have to write new controllers).
I'm a bit confused as to how to get started. I want to put my mobile version on a subdomain (m.myhost.tld
), however.. since my app is at www.myhost.tld
and I don't feel like copying it all over to another folder and maintain two, I'm a bit confused.
I know I can use the User Agent library in CodeIgniter to detect mobile browsers and load views accordingly; I just don't know how to get this working with a subdomain. Do I need to customize my app/config/routes.php
file here, or can I fix this with some .htaccess
magic? I have next to none experience with .htaccess
though. The only thing I know is how to remove my index.php from CI apps, and that's a copypasta snippet.
EDIT: I wonder if I can use a tutorial like this one to do what I want to do? It seems to be doing more or less the same thing, just with dynamic usernames instead of a simple 'm.'
EDIT 2: Some more information, I guess.
Say I detect mobile browsers using the User Agent library included with CodeIgniter. I want to direct these browsers to m.myhost.tld
. However, the content that I want to display on the mobile website comes from a controller called mobile
which I can also access through www.myhost.tld/mobile/
; so my question is if there is a way to route a URL like.. for example www.myhost.tld/mobile/about
to m.myhost.tld/about
. I'm not even sure if this is possible, teehee. Still learning!
I'll be grateful for any advice you can give me. Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想在不同的主机上共享相同的文件,你必须在你的Web服务器中分配你的站点的文档根文件夹,这是对静态文件的解释,但是是你理解的基础。
浏览器->主机:zyxxx[ip.ip.ip.ip] ->网络服务器 ->读取文件系统:文档根+浏览器请求路径,
因此如果您的文档根是:
并且请求是 /path-to-static/index.html 服务器尝试读取:
总之,您在中创建新主机 m.mysite.tld您的 Web 服务器并且您将文档根更改为与 www.mysite.tld 相同,您也可以使用主机别名指令,例如 Apache ServerAlias 指令。有很多关于如何配置 Web 服务器的文档。
您可以使用
$_SERVER['HTTP_HOST']
变量处理 php 中的主机名。如果您能指定更多,我可以提供更多帮助。
祝你今天过得愉快
If you want to share the same files in different hosts, you must assign the document root folder of your sites in your web server, this is an explanation for static files, but is the base to you understand.
browser -> host:z.y.xxx[ip.ip.ip.ip] -> web server -> read filesystem : document root + browser request path
so if your document root is:
and the request is /path-to-static/index.html the server try to read:
In conclution, you create the new host m.mysite.tld in your web server and you change the document root as the same of the you www.mysite.tld also you could use directives of host alias, like Apache ServerAlias directive. Have lot of documentation to how you could configure a web server.
You could handle the host name in php with
$_SERVER['HTTP_HOST']
variable.If you could specify more, I could help more.
have a nice day