带有 htaccess 的 CakePHP 子域

时间:2023-02-27
本文介绍了带有 htaccess 的 CakePHP 子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他应用程序的 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模板网!

上一篇:使用 htaccess 创建 SEO 友好的 url 下一篇:如何查看哪个复选框被选中?

相关文章

最新文章