Zend and modrewrite not working together properly - zend-framework

I'm setting up a Zend application, and went through the basic steps of setting up modrewrite on my development station, and writing an .htaccess file (shown below). Now, when I go to the root of my project, Zend works properly, calling the controller and displaying the appropriate page.
However, when I call any controller, I would expect it to redirect to the same index.php file, which would in turn direct it to the appropriate controller and action (e.g. myurl/controller/action would be read by myurl/index.php which would then redirect to the action appropriately).
I suspect that the issue is somehow related to how I've set up my .htaccess file, since calling the base url does work properly. But I haven't been able to figure out what's wrong. Does anyone have a suggestion?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)/(.*)$ index.php [NC,L]
RewriteRule ^(.*)/(.*)/(.*)$ index.php [NC,L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ index.php [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Okay, here's what it was.
My DOCUMENT_ROOT is set to /var/www while my application is running out of /var/www/app1 with everything set up in there. Because of the line RewriteBase / it was redirecting to the DOCUMENT_ROOT instead of to the APPLICATION_ROOT. I fixed it by changing that line to:
RewriteBase /app1
and now everything works perfectly.

Related

Zend Framework in subdirectory

I'm banging over the wall with this...
I need to establish a zend framework application inside a subdirectory.
i.e. the domain is tricell.ss and I want to use tricell.ss/tricell as application main directory.
Whate've done:
I've setted up in my config (application.ini) the:
resources.frontController.baseUrl = "/tricell"
I've made another var in my config
baseUrl = "/tricell"
In my Boostrap I've got this:
$router = new Zend_Controller_Router_Rewrite();
$frontController->setRouter($router);
$frontController->setBaseUrl($this->config->baseUrl);
$frontController->dispatch();
In my layout I'm trying to display image with using:
$this->baseUrl('/img/btn.png')
Here are my vhost configuration:
<VirtualHost *:80>
DocumentRoot "/HTDOCS/tricell/tricell/public_html"
ServerName tricell.ss
RewriteRule ^$ [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?tricell.ss$
RewriteRule ^(/)?$ tricell [L]
</VirtualHost>
here is my .htaccess:
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]
The problem is when I'm trying to display image nothing happens. When I'm trying to get the image by URL:
http://tricell.ss//tricell/img/btn.png
I'm getting info about wrong controller. How I can make this right? I can (without any problems) make configuration for root domain like tricell.ss and then get image i.e. tricell/ss/img/btn.png, but I want to work on subdirectory, and I can't do that with images. What I am doing wrong here?
I think the easiest thing you can do to get this working is this:
Start with a fresh ZF project, or remove all of your zend routes, and any special configuration in .htaccess or httpd.conf/httpd-vhosts.conf that you created.
Get rid of the baseUrl options you set up in your config file.
Place the index.php file and the .htaccess file in your /HTDOCS/tricell/tricell/public_html/tricell folder.
Place the rest of your project (controllers, models, config etc) outside of your web root. In this example I will assume they are placed in /HTDOCS/tricell/tricell/zfproject.
Edit index.php and change:
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// to:
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../zfproject/application'));
Now your application will run with the base directory being yoursite.com/tricell
IndexController.php's URL is going to be http://yoursite.com/tricell/index
You do not need to do anything special to tell Zend your base URL is "tricell". Just make sure in your view scripts to use $this->baseUrl() for your paths so you don't need to worry about manually adding /tricell to the beginning of everything, and if you ever move your project to another directory/path, nothing needs to be changed to deal with the new paths.
I've just manage to resolve my issue... it's quite simple but I was very confused by all this rewriting rules.
As saying always tricell is confusing let's make some changes to that as well.
So to achive something like tricell.ss/sub I would need to:
Put my whole application inside /HTDOCS/tricell/tricell/sub
Change my vhost from this line: DocumentRoot "/HTDOCS/tricell/tricell/public_html" to: DocumentRoot "/HTDOCS/tricell/tricell/"
Insert yet another .htaccess in my /HTDOCS/tricell/tricell/sub directory:
RewriteEngine On
RewriteRule ^.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /sub/ublic_html/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/sub/public_html/.$
RewriteRule ^(.)$ /sub/public_html/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public_html/.*$ /sub/public_html/index.php [NC,L]
This resolved my all issues.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} "/tour/"
RewriteRule (.*) /tour/index.php [L,END]
</IfModule>
The above code in the root folder domain.com/.htaccess worked for me. I placed it directly as the first thing in the htaccess before the "# BEGIN WordPress" parts.
having a Zend installation in the domain.com/tour/ subdirectory
wordpress running on the root domain.com/ level
The final knack was to have the RewriteRule point to the index.php of Zend if Request_URI matches /tour/

Running Zend Framework in a subfolder - Problems with default controller

I am starting a Zend application and I need to put it in a sub folder like this:
/subfolder
application
public
...
I did the following:
I set the base url in application.ini:
resources.frontController.baseUrl = "/subfolder"
I moved the .htaccess from the public folder directly into /subfolder, while changing the htaccess as follows:
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]
That's all I did. And everything works as expected. I can add controller blog with action view and can access it by calling:
/subfolder/blog/view
the url view helper and stuff work right too. The only thing that does not work is the default controller. So when I enter:
/subolder/
I get 403 (forbidden). If I enter
/subfolder/index
...it works. What am I missing?!
Thanks in advance!
teebee
you do not need to make any change in your zf public folder, just create an .htaccess file into the "/subfolder" directory with the following lines
# Rewrite rules
RewriteEngine on
RewriteRule (.*) ./public/$1
Replace
RewriteRule ^.$ public/index.php [NC,L]
with
RewriteRule ^.$ http://addressto/index.php [R=302,NC]
or
RewriteRule ^.*$ /public/index.php [NC,L]
i'm not sure if putting ZF folders other than public into publicly accessible folder is right.It will impose your site on security flaws. You must put ZF'S public folder' contents into your server`s public folder and put other folders into a non-publicly accessible folder.If you need more that one instance of your ZF application you may use Zend module system.Please refer to zend online documentation for more details.

Zend application problem on iis6

When I'm passing the URL like http://localhost/home/index/index in my application I'm getting the module=>default, controller=>index, action=>index. Whatever may be the URL i'm getting the same thing. I'm running the application on IIS6 in windows 2003 platform. If i'm overriding the module as 'home' in Zend_Controller_Request_Abstract=>getModuleName() method then my home page is coming fine. I'm unable to get where exactly it is overriding the value to 'default','index','index'. Can any one help me?
Thanks in advance
Update:
The problem is with $_SERVER['HTTP_X_REWRITE_URL']. I'm getting the same URI into both $_SERVER['HTTP_X_REWRITE_URL'] and $_SERVER['REQUEST_URI'] like /public_mvc/index.php. I'm not overriding the value of $_SERVER['HTTP_X_REWRITE_URL'] anywhere in my code. My .htaccess code is like this.
RewriteEngine on
RewriteRule ^admin-mvc(.*)$ public_mvc/admin.php [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^[\w/\%](?:.(?!(?:js|ico|gif|jpg|png|css|html)$)[\w\%]$)? public_mvc/index.php [I]
RewriteRule ^$ /home [r=301,nc]
I'm using ISAPI Rewrite 3 for URL Rewriting.
this is how Zend Framework handles the "default" module.
You can access your root path like this:
domain.tld/
domain.tld/index
domain.tld/index/index
domain.tld/default/index/index
all request will result in
module: default
controller: index
action: index
Update 1:
Probably there is a static route for module home configured
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.static
which sends requests for
/home/index/index
to
/default/index/index
Update 2:
Make sure you have Isapi_Rewrite installed as indicated here (search for IIS):
http://framework.zend.com/manual/en/zend.controller.router.html
Update 3:
Add this rewirte rule to your .htaccess (as indicated in Zend article of Update 2):
RewriteRule ^[\w/\%]*(?:\.(?!(?:js|ico|gif|jpg|png|css|html)$)[\w\%]*$)? /index.php [I]
Update 4:
Try the following code in your .htaccess (it is the recommendation from Zend article of Update 2)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^[\w/\%]*(?:\.(?!(?:js|ico|gif|jpg|png|css|html)$)[\w\%]*$)? /index.php [I]
If this does not work, I'm not sure what the solution is. I'm not using IIS and not familiar with the particularities there...

How to avoid index.php in Zend Framework?

I'm using the Zend Router and so things like (/ and /index.php) or (/about and /index.php/about) ends up as the same here.
However, /index.php/whatever should not exist as it is the exactly same resource as /whatever so it doesn't make a sense the duplication.
How do I avoid this?
Even http://zendframework.com/manual/en/zend.controller.router.html and http://zendframework.com/index.php/manual/en/zend.controller.router.html both exists. It doesn't make any sense at all...
1) Don't link to it (like the urls in your question) and let it be a hidden feature. It won't matter, basically.
or
2) Add a rewrite-rule to strip out /index.php from any request containing it. You have to be sure to forward it to index.php so that you still can handle it, and you probably want to send a status code beginning with 3.
Seems that your mod_rewrite is not configured properly (is mod_rewrite enabled in Apache? Is .htaccess configured properly?). When it would be, you wouldn't have to type index.php at all.
Edit:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^index.php [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

ISAPI ZEND url rewrite adding extra "/index.php/" into url

We recently reinstalled our web site on our server, the sys admin says it is an exact rebuild, and it indeed looks to me that it is, but there is something different going on. I did not originally develop the site, and those who did are no longer available.
urls for the admin site are now
//admin.site.com/index.php/schedules
and used to be, and should be
//admin.site.com/schedules
The extra index.php is getting inserted in the url. But all links within the admin site do not include the index.php so they do not work. If you insert the index.php into any url, it works. Where did this index.php come from and how do I eliminate it from the rewrite rules so the links will work.
This is what my httpd.conf file reads:
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.56
# turn on rewriting RewriteEngine On RewriteBase /
# for all files not found in the file system,
# reroute to "index.php" bootstrap script,
# keeping the query string intact.
RewriteCond %{HTTP_HOST} ^admin.site.com$
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{HTTP_HOST} ^admin.site.com$
RewriteRule ^.*$ index.php [NC,L]
Thank you! Any help would be greatly appreciated.
You need to implement and enable ApacheModrewrite. And then put a .htaccess file in your admin.site.com folder
Its content will be :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]