Redirect request to public directory zend - zend-framework

I have a sub domain to which i have deployed my zend application, the problem is it points to the directory say abc and i cannot get it changed to point it to public directory is there a way i can put a .htaccess file on the root which will redirect to the public directory? can it be done? is there another way i can get it done
my index.php inside public dir
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
my .htaccess inside public dir
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
currently the directory structure looks like
/myroot
-.settings
-application
-docs
-library
-public
-tests
-.buildpath
-.proect
-.zfproject.xml

RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
no errors and no problems..

I hope I get your domain setup correctly. If you have this setup
sub.domain.net --> /myroot
you will need an .htaccess in that (/myroot) folder because it is the base of your http requests. It should look like this
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ public/index.php [NC,L]
The RewriteBase directive should find all request in the RewriteCond and applies mainly to the first RewriteRule. It basically sets (rewrites) the relative root for all HTTP path request. The second RewriteRule finally triggers your application which is a local path. (I hope I am not wrong about this last rule. If that doesn't work try without the public. I can't test it but I had got an app to work like this a few years back.)

Related

Zend instance in a subfolder of another Zend

Hi i have a Zend installation in main domain.
Now i want to make a subfolder and run Zend for some testing purposes. I have copied all files from main site to subfolder named www2. But when i call the subfolder like domain.com/www2 i think the main Zend instance get invoked and produces a Message: Invalid controller specified (www2) error?
my .htaccess of main Zend is
SetEnv APPLICATION_ENV development
RewriteEngine on
RewriteCond %{REQUEST_URI} "/www2/"
RewriteRule (.*) $1 [L]
and Subfolder is like
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
What i am doing Wrong. I have full access to server.
in subfolder Zend in file application/Bootstrap.php add LIKE this:
protected function _initRoutes()
{
$this->bootstrap('frontcontroller');
/**
* #var Zend_Controller_Front $front
*/
$front = $this->getResource('frontcontroller');
/**
* #var Zend_Controller_Router_Rewrite $router
*/
$router = $front->getRouter();
$router->addRoute('www2',
new Zend_Controller_Router_Route(
'www2/:controller/:action',
array('controller' => 'index',
'action' => 'index'))
);
}
But may be you need another route...
UPD1:
first .htaccess like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/www2*
RewriteRule ^.*$ www2/index.php [NC,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
You could create a subdomain www2.domain.com pointing the www2/public directory and restore all .htaccess files to their normal single-site state.
This way, the www2 copy is completely standalone app with the same url and file structure as the original www site.

Including Zend Framework library in php.ini on bluehost server

I'm not able to set up include paths for ZF library correctly on Bluehost server.
Here's the directory structure:
/root_directory
.htaccess
__/Zend
__Application(from ZF)
__library(from ZF)
__/public_html
__/public(the ZF public folder)
I removed the .htaccess from public folder and placed it in the root_directory.
contents of .htaccess:
RewriteEngine On
RewriteRule ^.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.$
RewriteRule ^(.)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
Contents of index.php in public folder:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$FrontController = Zend_Controller_Front::getInstance();
$Router=$FrontController->getRouter();
Zend_Layout::startMvc(array(
"layout" => "layout",
"layoutPath" => "layouts/scripts"
));
$plugin=new Zend_Controller_Plugin_ErrorHandler();
$plugin->setErrorHandler(array("controller" =>'ApplicationError',
"action"=>'index'));
$FrontController->registerPlugin($plugin);
$application->bootstrap()
->run();
Kindly help me deploying my project on server. If there is anything else I need to mention please let me know.
See if doing the following gets you up and running:
Move contents of public_html/public into public_html and delete the public folder
Delete root_directory/.htaccess
Create public_html/.htaccess
In public_html/.htaccess, put:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Your index.php file looks fine, no need to change APPLICATION_PATH.
Basically, the only change is that instead of actually using the public folder that came with Zend Framework, you replace it with public_html instead since that is really your public folder.

Zend Framework htaccess Backend / Frontend

In my current ZF project, I've two public access points (files) ready to handle & process coming requests, that's: index.php & admin.php
I need all URLs containing '/admin' to be routed to admin.php while any other URLs doesn't contain '/admin' to be routed to index.php
Knowing that default ZF htaccess contains the following rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
How this can be implemented?
I want to start by saying that I full heartedly agree with AurelioDeRosa. I do believe that routes will be a better choice. However that is your choice not mine and this .htaccess code should do the trick you want.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*(admin).*$ admin.php [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Note however that this script will catch anything that contains admin so it might break your frontend. for example, if you want to list all your users with administrative rights the url yoursite.com/users/listby/admin/ will redirect the request to admin.php, and so will urls like /administrator/ or /adminininini

deploying zend framework application in mediatemple GS

i am having trouble deploying my zend framework application to mediatemple GS hosting.
I uploaded my application folder to ~/domains and renamed it to mysite.net folder.
Inside it, i created a symbolic link html which points to public directory in zend framework application folder. But the application is not working so far. Generally, mediatemple points a host to ~/domains/host/html path so my domain mysite.net should point to ~/domains/mysite.net/html.
Am i doing something wrong or is there more to this ? I am getting a 403 error when trying to access mysite.net
Update: Following rewrite rules make the base url http://mysite.net loadable but controllers dont work now. accessing http://mysite.net/controller sends page not found error :S
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
Update: Its solved now. The trick is to add above .htaccess file to the application root folder and also keep the original .htaccess in public directory. Together they do wonders.
I've found the solution myself:
create a .htaccess file in application root folder:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
Also keep the original .htaccess in the /public folder. I delete this at first which made things worse.

.htaccess: how to redirect img,js.. from /www/ to /www/public/?

I have zend framework htaccess now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ public/index.php [NC,L]
But I don't know how to redirect all "link" parameters like js, css, img.
Solution one:
Your web server should point to public/ directory, not to root directory of ZF project.
Solution two (better one I think):
Your web server points to ZF project directory, or even ZF project is in sub directory.
Like http://localhost/zf-project/ .
.htaccess in zf-project directory (right near public/, application/, library/ ...):
RewriteEngine On
RewriteRule ^(.*)$ public/$1?%{QUERY_STRING} [QSA,L]
.htaccess in public/ directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
And in application/configs/application.ini you set
resources.frontController.baseUrl = "/zf-project/"
Here is my .htaccess which is work perfect:
DirectoryIndex /public/index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^js/(.*)$ public/js/$1 [L]
RewriteRule ^img/(.*)$ public/img/$1 [L]
RewriteRule ^css/(.*)$ public/css/$1 [L]
RewriteRule ^.*$ public/index.php [NC,L]