rishijd
rishijd

Reputation: 1334

Zend Framework setting up the htaccess file

I have been using the Zend Framework for years but have realised some crucial problems with our error handling that we are now fixing. (I posted a different question here: Why my site is always using the ErrorController for all types of errors irrespective of HTTP Status code? explaining the story there).

My question here is a quick one. What does a common .htaccess file of Zend Framework look like?

According to the latest ZF documentation,

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]

However, the above is new to me - can someone explain what it does exactly? My current .htaccess file has a lot of 301 redirect code but for the purpose of this post I'll only paste the relevant information here:

ErrorDocument 404 http://www.mydomain.com/pagenotfound/
ErrorDocument 503 http://www.mydomain.com/service-unavailable/

RewriteCond %{REQUEST_URI} !^/liveagent
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule !\.(js|ico|gif|GIF|jpg|JPG|jpeg|png|PNG|pdf|css|html|xml|swf|php|mp3|mp4|webm|ogv|f4v|flv|txt|wsdl|css3|ttf|eot|svg|woff)$ index.php

The above has been working fine for us, and basically disallows the "liveagent" and "blog" (Wordpress) directories from running with Zend, but I realise I now need to make the following change:

ErrorDocument 404 definitely has to be removed from the code, as Zend Framework should handle all errors. However, when I remove this, going to a URL like www.mydomain.com/this-does-not-exist.php results in a 404 error standard Apache page - it does not load the ZF or the ErrorController. This is because of the "php" exclusion in the above RewriteRule. I do not simply want to remove this since we sometimes want to be able to access php files on the root, such as a separate "holding.php" file which we use for putting the site on maintenance mode.

What is the standard practice? Should I remove the php extension? However this will not solve other 404's like www.mydomain.com/this-does-not-exist.css which is also an exclusion (i.e. CSS) in the above RewriteRule.

Therefore, should I completely change the above to Zend's new code for .htaccess as I mentioned above?

If so, I'm a sort of beginner at htaccess - how can I modify that .htaccess code to allow CSS, JS, video files etc. and the blog and liveagent directories to be excluded from the Zend Framework?

Upvotes: 1

Views: 4088

Answers (1)

drew010
drew010

Reputation: 69927

I'd switch to the standard ZF rewrite rules instead of the one you have which uses a long regex to redirect to index.php.

Here is an explanation of what the standard .htaccess rules do:

RewriteCond %{REQUEST_FILENAME} -s [OR] # The request is a regular file with size > 0
RewriteCond %{REQUEST_FILENAME} -l [OR] # The request is to a file that is a symlink
RewriteCond %{REQUEST_FILENAME} -d [OR] # The request is to a directory that exists

# if any of the above conditions are true, then simply handle the request as-is
RewriteRule ^.*$ - [NC,L]

# if none of the above match, then rewrite to index.php
RewriteRule ^.*$ index.php [NC,L]

These default ZF rules don't prevent you from accessing existing php files or any other files that are accessible from your document root. If the file requested exists, then the request for that file is served as is. If the file requested does not exist, then the request is forwarded to index.php

Once the request is forwarded to ZF, if there is no matching route, then the ZF ErrorHandler is called and a 404 page (from ZF) is served.

Using the stock ZF rules won't prevent you from having the desired behavior in your application and server settings, and should be a bit more efficient that the regex you currently have. The only things that will really change is that now requests for files that don't exist will be handled by ZF's error handler and no longer by Apache.

Hopefully that answered your question, if not feel free to comment for clarification.

Upvotes: 2

Related Questions