Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP, CGI and mod_rewrite

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.

like image 758
bfncs Avatar asked Aug 22 '12 20:08

bfncs


2 Answers

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?

like image 124
L. Sanna Avatar answered Sep 19 '22 17:09

L. Sanna


/~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

like image 37
Ceeram Avatar answered Sep 19 '22 17:09

Ceeram