我有一个住在/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 -
- mysite.myserver→ myserver/m/mysite/
- mysite.myserver/home→ myserver/m/mysite/home
- mysite.myserver/foo/bar?baz=true→ myserver/m/mysite/foo/bar?baz=true
- mysite.myserver → myserver/m/mysite/
- mysite.myserver/home → myserver/m/mysite/home
- mysite.myserver/foo/bar?baz=true → myserver/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\$ [NC] RewriteRule ^(.*)$ myserver/m/%1/$1 [L] </IfModule>据我所知,主要问题是$_SERVER['REQUEST_URI']:
As far as I can tell, the main issue is with $_SERVER['REQUEST_URI']:
- 如果我直接浏览至myserver/m/mysite/home,则为$_SERVER['REQUEST_URI'] = /m/mysite/home.
- 如果我使用上面的.htaccess文件浏览到mysite.myserver/home,则为$_SERVER['REQUEST_URI'] = /home.
- If I browse to myserver/m/mysite/home directly, $_SERVER['REQUEST_URI'] = /m/mysite/home.
- If I browse to mysite.myserver/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\$ [NC] RewriteRule !^m/ myserver/m/sites/%1%{REQUEST_URI} [NC,L,P]更多推荐
将子域重定向到CakePHP操作
发布评论