CakePHP, CGI and mod_rewrite
Asked Answered
G

3

6

I have to run my CakePHP 2.1 application in an environment with CGI-PHP and without the ability to declare apache aliases. I want to redirect requests to a subdomain to CakePHP with mod_rewrite, but this doesn't work out.

Current setup

  • Webroot is ~/user/public_html
  • CakePHP is in ~/user/public_html/cakephp/
  • CakePHP should be requested at dev.mydomain.tld

What I have until now is this (all paths relative to webroot):

  • ~/user/public_html/.htaccess

    RewriteEngine on
    Options +FollowSymlinks
    RewriteCond %{HTTP_HOST} ^(www\.)?dev\.mydomain.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/cakephp/app/webroot/ [NC]
    RewriteCond %{REQUEST_URI} !/$
    RewriteCond %{DOCUMENT_ROOT}/cakephp/app/webroot%{REQUEST_URI}/ -d
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R,L]
    RewriteCond %{HTTP_HOST} ^(www\.)?dev\.mydomain.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/cakephp/app/webroot/ [NC]
    RewriteRule ^(.*)$ /cakephp/app/webroot/$1 [L]
    
  • ~/user/public_html/cakephp/app/webroot/.htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
    

The Problem

Requests are somehow not routed correctly (the application runs without problems in my development environment with PHP as a module and an Virtual Host at the /cakephp/app/webroot/ level). When I request the home page at dev.mydomain.tld I only get an Error, Cake is telling me, that the CakephpController is missing.

Debug information

  • the interesting parts of $_SERVER debugged as the first line in /cakephp/app/webroot/index.php

      [REDIRECT_REDIRECT_REDIRECT_STATUS] => 200
      [REDIRECT_REDIRECT_STATUS] => 200
      [REDIRECT_HANDLER] => php-script
      [REDIRECT_STATUS] => On
      [HTTP_HOST] => dev.mydomain.tld
      [HTTP_CONNECTION] => keep-alive
      [SERVER_SOFTWARE] => Apache/2.2.21 (Unix)
      [SERVER_NAME] => dev.mydomain.tld
      [SERVER_ADDR] => 192.0.43.10
      [SERVER_PORT] => 80
      [DOCUMENT_ROOT] => /home/user/public_html
      [SCRIPT_FILENAME] => /home/user/public_html/cakephp/app/webroot/index.php
      [REDIRECT_URL] => /cakephp/app/webroot/index.php
      [GATEWAY_INTERFACE] => CGI/1.1
      [SERVER_PROTOCOL] => HTTP/1.1
      [REQUEST_METHOD] => GET
      [QUERY_STRING] => 
      [REQUEST_URI] => /
      [SCRIPT_NAME] => /cakephp/app/webroot/index.php
      [_PHP5_WORK_DIR] => /home/user/public_html/cakephp/app/webroot
      [PHP_SELF] => /
      [ORIG_PATH_INFO] => 
      [ORIG_PATH_TRANSLATED] => /home/user/public_html/cakephp/app/webroot/index.php
      [PATH_INFO] => /cakephp/app/webroot/index.php
    
  • the interesting parts of the CakeRequest object passed to the dispatcher:

      url => 'cakephp/app/webroot/index.php'
      base => '/cakephp'
      webroot => '/app/webroot/'
      here => '/cakephp/cakephp/app/webroot/index.php'
    

The question

So, what I don't get here, is why the CakeRequest object has references to my folder structure while $_SERVER['REQUEST_URI'] == '/'. What would I have to do, to get this right? And in the first place, where should I look for the problem: in the mod_rewrite directives or in CakePHP itself? I tried some things, including setting the RewriteBase in alle the .htaccess files and different settings for App.baseUrl in the Configuration object, but nothing seemed to help here.

I would be really thankful if somebody could give me a pointer on how to solve this problem.

Greengrocery answered 22/8, 2012 at 20:26 Comment(0)
A
2

I would be really thankful if somebody could give me a pointer on how to solve this problem.

Then I'll give you a pointer as I don't know the answer. :)

Have you read this question where I had a problem quite similar to yours?

Accommodative answered 27/8, 2012 at 17:32 Comment(3)
Thanks for the pointer! I didn't read it before, but you're right, it looks quite similiar. It's a pity that the proposed solution doesn't help in my case, too. I'm probably going to link my app/webroot to the hard webroot to get around this, which will for sure only move the problem to the future. I would still be really interested in solving this, even more after seeing you had the same problem. P.S.: No WAMP involved here.Greengrocery
You could read this too, especially the comments, that's how I solved my problem: bakery.cakephp.org/articles/BBBThunda/2010/02/25/…Accommodative
ALS, I finally picked you answer because it came closest to what I needed and I didn't want to waste the points. It's pretty sure, that there was some strange server misconfiguration by the hoster involved and they're working on fixing it. So long, I'm hard overwriting the values in $_SERVER with reasonable defaults. That's really, really ugly, but it works as a workaround until that server config has been fixed.Greengrocery
G
1

/~username urls are generated with mod_userdir, mod_rewrite however strips ~ chars

So use default cake htaccess files, and add:

RewriteBase /~username/path/to/cake

to all involved htaccess files

Glendaglenden answered 23/8, 2012 at 15:3 Comment(1)
I misread ~/username filepath for /~username url please ignore this answerGlendaglenden
M
1

Traditionally one would redirect requests to a CakePHP application to the /cakephp/app/ level.

The fact that it wishes to interpret cakephp as a controller name is a giveaway that this is the issue.

Your problem is likely related to ALS's but not exactly the same. What you actually want because you intend for your cakephp app to appear to reside at the root of the server when it actually does not is:

RewriteBase /

The other potential issue is that your dev environment is never exposed to the any potential side-effects related to the .htaccess file under /cakephp/app.

What I would try is first adding RewriteBase relative to the /cakephp folder

  1. In app/.htaccess: /
  2. In app/webroot/.htaccess: /webroot/
Merriman answered 29/8, 2012 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.