问题描述
我用在其他应用程序我htaccess文件以下规则从一个文件夹将用户重定向到一个子域,但访问该子域时加载该文件夹的内容。
I've used the following rules in my htaccess file in other applications to redirect users from a folder to a subdomain but load the content from that folder when accessing that subdomain.
# REWRITE SUBDOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^admin\.cameron\.com$
RewriteRule !^admin/? admin%{REQUEST_URI} [NC,L]
# REWRITE FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/admin/([^\s]*) [NC]
RewriteRule ^ http://admin.cameron.com/%1 [R=301,L]
所以,如果我去: http://cameron.com/admin
我结束了在 http://admin.cameron.com /
但内容是从 http://cameron.com/admin
然而,这不适用于CakePHP的2.x的工作,因为它改写显然...
However this doesn't work for CakePHP 2.x because of its rewriting apparently...
在 /应用程序/ Web根目录
我的htaccess文件我有:
In my htaccess file in /app/webroot
I have:
<IfModule mod_rewrite.c>
RewriteEngine On
# CAKEPHP RULES
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
# REWRITE SUBDOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^admin\.cameron\.com$
RewriteRule !^admin/? admin%{REQUEST_URI} [NC,L]
# REWRITE FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/admin/([^\s]*) [NC]
RewriteRule ^ http://admin.cameron.com/%1 [R=301,L]
</IfModule>
如果我去: http://cameron.com/admin
它只是尝试加载AdminController并不会重定向你,如果我去: http://admin.cameron.com/
我只是得到一个500内部服务器错误。
If I go to: http://cameron.com/admin
it just tries to load the AdminController and doesn't redirect you, and if I go to: http://admin.cameron.com/
I just get a 500 Internal Server Error.
这是如何得到这个工作CakePHP的任何想法?
Any ideas on how to get this working for CakePHP?
推荐答案
CakePHP的,几乎任何其他PHP框架解析$ _ SERVER ['REQUEST_URI']为了航线您的要求,以特定的控制器
CakePHP and almost any other PHP framework parse $_SERVER['REQUEST_URI'] in order to route your request to particular controller
更多信息:,
所以你只需要具有相同的请求URI参数为您重定向得到应用工作。
so you simply need to have the SAME request URI parameters for your redirect to get app working.
有关旧的位置,这是/ admin的,对于新的位置应该是相同的,但你不希望通过它,所以最好改变这样的任务:
For your old location it was "/admin", for new location it should be the same, but you do not want to pass it, so it is better to change the task like this:
当你去admin.site.com你将被重定向到site.com/admin~~V。
"When you go to admin.site.com you will be redirected to site.com/admin".
这可以简单地做过这样的:
This can be done simply like this:
重写规则^([^。] +)\\。例如\\ .COM http://example.com/$1
这不是规则的工作的例子,因为你还需要重写的子域的请求URI来得到的一切工作,这取决于你的需求,但可以水木清华这样的:
It is not a working example of rule because you also need to rewrite subdomain's request URI to to get everything working and it depends on your requirements, but can be smth like this:
重写规则^([^。] +)\\。例如\\ .COM(。*)http://example.com/$1/$2
UPD:真实的例子
$ cat app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^admin.example.com
RewriteRule ^(.*)$ http://example.com/admin/$1 [P,L,NC]
</IfModule>
这篇关于CakePHP的子域与htaccess的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!