問題描述
我在其他應用程序的 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/
So if I go to: http://cameron.com/admin
I end up on 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...
在 /app/webroot
中的 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
更多信息:https://github.com/cakephp/cakephp/blob/2.6/lib/Cake/Network/CakeRequest.php#L230,
因此您只需要為重定向提供相同的請求 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".
"When you go to admin.site.com you will be redirected to site.com/admin".
這可以像這樣簡單地完成:
This can be done simply like this:
重寫規則 ^([^.]+).example.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:
重寫規則 ^([^.]+).example.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>
這篇關于帶有 htaccess 的 CakePHP 子域的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!