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.

bfncs
  • 10,007
  • 4
  • 32
  • 52

3 Answers3

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?

Community
  • 1
  • 1
L. Sanna
  • 6,482
  • 7
  • 33
  • 47
  • 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. – bfncs Aug 27 '12 at 19:38
  • You could read this too, especially the comments, that's how I solved my problem: http://bakery.cakephp.org/articles/BBBThunda/2010/02/25/troubleshooting-cakephp-installation-issues-related-to-apache-2-mod_rewrite-for-beginners – L. Sanna Aug 27 '12 at 20:02
  • 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. – bfncs Oct 02 '12 at 15:40
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

Ceeram
  • 748
  • 4
  • 9
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/
Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71