Redirigir subdominio a la acción CakePHP
Frecuentes
Visto 1,067 veces
3
Antecedentes
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
→http://myserver.com/m/mysite/
http://mysite.myserver.com/home
→http://myserver.com/m/mysite/home
http://mysite.myserver.com/foo/bar?baz=true
→http://myserver.com/m/mysite/foo/bar?baz=true
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 ^(.*)$ http://myserver.com/m/%1/$1 [L]
</IfModule>
As far as I can tell, the main issue is with $_SERVER['REQUEST_URI']
:
- Si busco
http://myserver.com/m/mysite/home
directamente,$_SERVER['REQUEST_URI'] = /m/mysite/home
. - Si busco
http://mysite.myserver.com/home
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?
1 Respuestas
4
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/ http://myserver.com/m/sites/%1%{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 php apache .htaccess cakephp mod-rewrite or haz tu propia pregunta.
Well, it doesn't work right now, with the way the
.htaccess
file is written. - Glen BallietI'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 thesites
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@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? - Glen Balliet