Redirigir subdominio a la acción CakePHP


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 -

  • http://mysite.myserver.com

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).

Aquí está mi intento actual:

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

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

  • Si busco directamente, $_SERVER['REQUEST_URI'] = /m/mysite/home.
  • Si busco usando el .htaccess archivo de arriba, $_SERVER['REQUEST_URI'] = /home.

El problema

Because of the issues with $_SERVER['REQUEST_URI'], my routes aren't parsing correctly: it's trying to take the user to /home más bien que /m/mysite/home como se desee.

How can I change my rewrite rule to make this work properly? Is there another way to accomplish this?

preguntado el 03 de mayo de 12 a las 21:05

Well, it doesn't work right now, with the way the .htaccess file is written. -

I'm not sure where you think $_SERVER['REQUEST_URI'] comes into play, but that's just a PHP constant holding the part behind the domain name (e.g. /index.php). It doesn't have anything to do with rewriting. A quick peek suggests that the sites bit after the /m/ in your RewriteRule is not mentioned in the rest of your question, are you sure it's supposed to be there? -

@Oldskool: thanks for pointing out the sites bit, but that was just a typo. The reason I pointed out $_SERVER['REQUEST_URI'] is because, ideally, I'd like to "spoof" it somehow. I believe that CakePHP is using that variable to parse the route, which is why (even though my .htaccess gobernar técnicamente points to the right URL) it isn't working. Is there a way to fix this with .htaccess rules at all? -

1 Respuestas

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 habilitar mod_proxy in your Apache and restart it. Once that is done use following code in your site root .htaccess:

RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule !^m/{REQUEST_URI} [NC,L,P]

contestado el 11 de mayo de 18 a las 19:05

This worked perfectly, and did exactamente lo que estaba buscando. ¡Gracias! - Cañada Balliet

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.