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:
- at config file, I set a global definition:
define('WWW_ALIAS', '/projectB');
- at PHP side, it should have no links at Models, Components. Almost wrong link is at Controllers with redirect(), so let search then replace:
$this->redirect('/ =replace=>; $this->redirect( WWW_ALIAS .'/
- at View, there’re some cases can make wrong links:
- form submit
- search for keyword ‘form’ and check for ‘action’ link.
- image
- src = ‘/ ==> src =<?php echo WWW_ALIAS;?>
- a href
href = '/ ==> href ='/
- javascript
- 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.
- form submit
- at webroot/css
- image link can be wrong: search for “url(/” => change to relative link.
- at webroot/js – in my case, an app with many javascript, ajax, so a lot of links are wrong.
- make global javascript variable at top of page ( should located at header/ layout file)
var WWW_ALIAS ='';
- in js files, search for images “/img”, “location.href”, ajax request ajax( ..
url =WWW_ALIAS+'/;
'/img..' ==> '/' + WWW_ALIAS + ''img..'
href ='/ (in js we generated html too)
- make global javascript variable at top of page ( should located at header/ layout file)
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.