带有 htaccess 的 CakePHP 子域

CakePHP subdomains with htaccess(带有 htaccess 的 CakePHP 子域)
本文介绍了带有 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 子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)