Redirect Subdomain to CakePHP Action

Background

I have a CakePHP application that lives in /m/. I want to write a root level file .htaccessthat redirects the "subdomains" for the site as parameters for actions.

For example: I want to write a rewrite rule that will lead to redirects like this -

  • http://mysite.myserver.comhttp://myserver.com/m/mysite/
  • http://mysite.myserver.com/homehttp://myserver.com/m/mysite/home
  • http://mysite.myserver.com/foo/bar?baz=truehttp://myserver.com/m/mysite/foo/bar?baz=true

Ideally, this redirect should be invisible to users (I don't want to use 301 because I don't want to change the URL).

Here is my current attempt:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^(.*)\.myserver\.com$ [NC]
  RewriteRule ^(.*)$ http://myserver.com/m/%1/$1 [L]
</IfModule>

As far as I can tell, the main problem is $_SERVER['REQUEST_URI']:

  • If I just look through http://myserver.com/m/mysite/home, $_SERVER['REQUEST_URI'] = /m/mysite/home.
  • If I move to http://mysite.myserver.com/homeusing the file .htaccessabove $_SERVER['REQUEST_URI'] = /home.

Problem

- $_SERVER['REQUEST_URI'] : /home, /m/mysite/home .

? ?

+3
1

, ( URL-, UR ) . , mod_proxy Apache . , .htaccess:

RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule (?!^m/$)^.*$ http://myserver.com/m/sites/%1%{REQUEST_URI} [NC,L,P]
+4

All Articles