everything about deploying/installing a cakephp project in a sub-directory.

Hi every body!

I’d like to share my real experience from the last project that I need to deploy my cakephp project as a sub folder.

Recently, I have a request to deploy our project as sub-folder. It heard simple. But in reality, I met some trouble, so I’d like to share with you.

Basicly, we will resolve it by .htaccess:

* condition:

  •        my DocumentRoot is /var/www/html/projectA/app/webroot  is running for septeni-technology.jp
  •        I’d like to setup for my cakephp project( projectB)  at sub directory at : projectA/projectB, so it  can be accessed as septeni-technology.jp/projectB/
  •        my deployment environment is LAMP, on window there’s small difference for variables but concept should be the same.

 

1. edit/create the .htaccess at your /var/www/html/projectA/

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^projectB/(.*)$ projectB/app/webroot/1$ [L,QSA]

..

</IfModule>

2.  add RewriteBase  to projectB/.htaccess  & projectB/app/.htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /projectB
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

 

owww.. if your source code is perfect,  maybe that’s enough, it’s working well! congrats!

But in my case, it’s a bit complicated that the source code is developed by many developers for years! Oh my goD!

so there’re many absolute links  in source code,  in my case, there’re hundreds.

  • redirect(‘/’), redirect(‘/profiles’) …
  • <img src = “/img/icon/icon_search.png”..
  • location.href = ‘/’ +..
  • < href = “/…”
  • ajaxSend(data, callback, url);

yes, maybe you guessed, almost located at js,css, .ctp files.

So my solution is search & replace all! It hear mad, but I can’t check all codes line by line. Here’re my steps:

  1. at config file, I set a global definition:
    1. define('WWW_ALIAS', '/projectB');
  2. at PHP side, it should have no links at Models, Components. Almost wrong link is at Controllers with redirect(), so let search then replace:
    1. $this->redirect('/  =replace=>; $this->redirect(  WWW_ALIAS .'/
  3. at View, there’re some cases can make wrong links:
    1. form submit
      1. search for keyword ‘form’ and check for ‘action’ link.
    2. image
      1. src = ‘/     ==> src =<?php echo WWW_ALIAS;?>
    3. a href
      1. href = '/   ==> href ='/
    4. javascript
      1. if javascript located in view file (rarely), you can use  <?php echo WWW_ALIAS;?> for wrong link, it’s depend on your business to find out pattern. I’m lucky, I don’t meet this.
  4. at webroot/css
    1. image link can be wrong: search for “url(/” => change to relative link.
  5. at webroot/js – in my case, an app with many  javascript, ajax, so a lot of links are wrong.
    1. make global javascript variable at top of page ( should located at header/ layout file)
      1. var WWW_ALIAS ='';
    2. in js files, search for images “/img”, “location.href”, ajax request ajax( ..
      1. url =WWW_ALIAS+'/;
      2. '/img..' ==>  '/' + WWW_ALIAS + ''img..'
      3. href ='/ (in js we generated html too)

With all of this steps, I fixed 99.9% links; there’re some other cases I only found by manual test. For example this type of code in js:

 location.pathname.split('/')[2] ..

This is reported by VietNC @ septeni-technolofy.

 

 

 

Add a Comment

Scroll Up