CakePHP, CGI and mod_rewrite

I need to run CakePHP 2.1 application in an environment with CGI-PHP and without the ability to declare apache aliases. I want to redirect requests to the CakePHP subdomain using mod_rewrite, but this will not work.

Current setting

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

What I have so far (all the ways regarding 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] 

Problem

Requests are somehow incorrectly routed (the application works without problems in my development environment with PHP as a module and virtual host at the level of /cakephp/app/webroot/ ). When I request the homepage in dev.mydomain.tld , I get an error message, Cake tells me that CakephpController is missing.

Debug information

  • 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 
  • interesting parts of the CakeRequest object are CakeRequest to the dispatcher:

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

Question

So, what I don’t understand is why the CakeRequest object has links to my folder structure, and $_SERVER['REQUEST_URI'] == '/' . What did I have to do to get it right? And first of all, where should I look for the problem: in the mod_rewrite directives or in CakePHP? I tried some things, including setting RewriteBase in .htaccess files and different settings for App.baseUrl in the Configuration object, but nothing helped here.

I would be very grateful if anyone could give me a pointer on how to solve this problem.

+6
source share
3 answers

I would be very grateful if anyone could give me a pointer on how to solve this problem.

Then I will give you a pointer as I do not know the answer. :)

Have you read this question , where did I have a problem very similar to yours?

+2
source

/ ~ User urls are generated using mod_userdir, mod_rewrite, however strips ~ chars

So, use the default htaccess files for the cake and add:

 RewriteBase /~username/path/to/cake 

all related htaccess files

+1
source

Traditionally, you can redirect requests to your CakePHP application to the /cakephp/app/ level.

The fact that he wants to interpret cakephp as the name of the controller is a fake that this is a problem.

Your problem is most likely related to ALS, but not exactly the same. What you really want is because you intend that your cakephp application will be displayed in the root directory of the server if it really is not:

 RewriteBase / 

Another potential problem is that your dev environment is never exposed to any potential side effects associated with the .htaccess file in /cakephp/app .

What would I try, first add a RewriteBase relative to the / cakephp folder

  • In the application /.htaccess: /
  • In app / webroot / .htaccess: /webroot/
+1
source

Source: https://habr.com/ru/post/923516/


All Articles