将子域重定向到CakePHP操作

编程入门 行业动态 更新时间:2024-10-22 11:04:49
本文介绍了将子域重定向到CakePHP操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个住在/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操作

本文发布于:2023-10-31 16:24:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   操作   将子域   CakePHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!