将子域重定向到CakePHP操作 [英] Redirect subdomain to CakePHP action

查看:89
本文介绍了将子域重定向到CakePHP操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个住在/m/的CakePHP应用程序.我想编写一个根级.htaccess文件,该文件将重定向站点的子域"作为操作的参数.

I have a CakePHP application that lives in /m/. I want to write a root-level .htaccess file which will redirect "subdomains" for the site as parameters to actions.

例如:我想编写一个重写规则,它将导致这样的重定向-

For example: I want to write a rewrite rule which will result in redirects like this -

  • http://mysite.myserver.comhttp://myserver.com/m/mysite/
  • http://mysite.myserver.com/homehttp://myserver.com/m/mysite/home
  • http://mysite.myserver.com/foo/bar?baz=truehttp://myserver.com/m/mysite/foo/bar?baz=true
  • http://mysite.myserver.comhttp://myserver.com/m/mysite/
  • http://mysite.myserver.com/homehttp://myserver.com/m/mysite/home
  • http://mysite.myserver.com/foo/bar?baz=truehttp://myserver.com/m/mysite/foo/bar?baz=true

理想情况下,用户应该看不到此重定向(我不想使用301,因为我不想更改URL).

Ideally, this redirect should be invisible to users (I don't want to use a 301, because I don't want to change the URL).

这是我目前的尝试:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^(.*)\.myserver\.com$ [NC]
  RewriteRule ^(.*)$ http://myserver.com/m/%1/$1 [L]
</IfModule>

据我所知,主要问题是$_SERVER['REQUEST_URI']:

As far as I can tell, the main issue is with $_SERVER['REQUEST_URI']:

  • 如果我直接浏览至http://myserver.com/m/mysite/home,则为$_SERVER['REQUEST_URI'] = /m/mysite/home.
  • 如果我使用上面的.htaccess文件浏览到http://mysite.myserver.com/home,则为$_SERVER['REQUEST_URI'] = /home.
  • If I browse to http://myserver.com/m/mysite/home directly, $_SERVER['REQUEST_URI'] = /m/mysite/home.
  • If I browse to http://mysite.myserver.com/home using the .htaccess file above, $_SERVER['REQUEST_URI'] = /home.

由于$_SERVER['REQUEST_URI']的问题,我的路由无法正确解析:它试图将用户转到所需的/home而不是/m/mysite/home.

Because of the issues with $_SERVER['REQUEST_URI'], my routes aren't parsing correctly: it's trying to take the user to /home rather than /m/mysite/home as desired.

如何更改我的重写规则以使其正常工作?还有另一种方法可以做到这一点吗?

How can I change my rewrite rule to make this work properly? Is there another way to accomplish this?

推荐答案

在正常情况下,您要问的内容(更改URL中的域名,但不要让浏览器看到URL更改)是无法实现的.但是,要使其成为可能,您可以在Apache中启用 mod_proxy并重新启动它.完成后,请在您的网站根目录.htaccess中使用以下代码:

What you're asking (change the domain name in URL but don't let browser see URL change) is not achievable under normal scenario. However to make it possible you have enable mod_proxy in your Apache and restart it. Once that is done use following code in your site root .htaccess:

RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule !^m/ http://myserver.com/m/sites/%1%{REQUEST_URI} [NC,L,P]

这篇关于将子域重定向到CakePHP操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆