Zend SetEnv in .htaccess not working - php

I installed Zend on my ubuntu homeserver. In my .htaccess file i have the following code:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
When i echo APPLICATION_ENV in my index.php in the public folder, APPLICATION_ENV is not set.
What am i doing wrong?
Mod rewrite is enabled in apache.

In order to use SetEnv within a .htaccess file, I believe you need to set...
AllowOverride FileInfo
...within the relevant virtualhost directory block. (And then restart the httpd service as per usual.)
Additionally, depending on how you're running PHP, it's possible that such information is being stripped out. (e.g.: suexec will effectively remove all non-HTTP* environment vars.)

First You need mod_env installed (and mod_rewrite)
Double check if Apache has the module(s) loaded with either:
apachectl -l
or
httpd -l
the command might be different according to OS distribution i believe.
For the .htaccess file to work you need AllowOveride All which can be set in different configuration files (and "parts" depending on the way your OS as Apache configured. Your best option is to find out if it is a htaccess file issue or an environment one. Set anywhere in main configuration file:
SetEnv TESTME foobar
restart Apache and echo TEST from a PHP script. If you got it, debug the .htaccess
Note 1: Restrict in your virtual host the usage of .htacess to the folder where you really need it or move the configurations to a virtual host.
Note 2: You probably don't need all those rewrite rules if you have Apache mod_dir installed. Have a look at it

You can also use:
RewriteRule .* - [E=ENV_NAME_HERE:ENV_VALUE_HERE]

I prefer to put SetEnv in the httpd.conf file for the local server, so that all my local ZF apps run in development mode, and when I push to the staging server it has a flag to Set the environment to staging, then the production server has no such setting so it defaults to production...
This way I can keep the same .htaccess file in source control and when I push it out to the different servers it behaves as expected.
middaparka is right about suExec... suExec prevents you from setting custom environment variables in your .htaccess file - according to remi at the WebFaction forum.

If you are running your home server on a Mac or have suEXEC enabled, there are security measures in place to strip any non-http environment variables that are defined, therefore they won't appear in $_ENV['APPLICATION_ENV']. You CAN, however access these through a PHP function to get the same thing.
$var = apache_getenv('APPLICATION_ENV');
You could even define the environment variable within PHP if you want, although not recommended.
$_ENV['APPLICATION_ENV'] = apache_getenv('APPLICATION_ENV');
I have an almost identical config to what you have, and we just define a global:
define('APPLICATION_ENV', apache_getenv('APPLICATION_ENV'));
Once you do that, it should work as desired.

Related

.htaccess url rewrite not passing parameter - could there be apache settings somewhere that is preventing this?

Okay, so this problem has completely stumped me and the other devs I work with. Here is the rundown:
I have a local dev environment setup with Mac Apache2 pointed at /Users/myusername/Sites/
Within /Sites I have two folders, /site-1 and /site-2, both of which have virtual hosts pointed at them site-1.dev & site-2.dev. Both site-1 and site-2 are running local installs of PerchCMS.
Within /site-2 I have an .htaccess file which I am trying to set up a URL rewrite that takes the URL /detail/slug-here and translates it into /detail.php?s=slug-here
I have tried the following rewrites (at the suggestion of PerchCMS support) and both have failed to pass the s param:
RewriteRule ^detail/([a-zA-Z0-9-/]+)$ detail.php?s=$1 [L]
RewriteRule ^site-2/detail/([a-zA-Z0-9-/]+)$ /site-2/detail.php?s=$1 [L]
Additional info:
Yes mod_rewrite is enabled in apache... in the same .htaccess file it totally works if I do a simple rewrite like this...
RewriteRule dangerzone.html index.php
One odd behavior that I've noticed is that if I remove everything from .htaccess I can still pull up detail.php by pointing my browser at /detail/test-item-1...(yes I have restarted my server) so its behaving as if there is still some sort of rewrite in place and loading detail.php sans param just as it continues to do with the rewrite in place - is this a clue that there is something off somewhere else in my server config? Note, RewriteRule dangerzone.html index.php does NOT work once it is removed from .htaccess.
Have this code in your site root .htaccess (inside /site-2/):
Options -MultiViews
RewriteEngine On
RewriteRule ^detail/([a-zA-Z0-9/-]+)/?$ detail.php?s=$1 [L,QSA,NC]
Important is to turn off MultiViews options here. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Local Apache Server mod_rewrite/pretty links issue

I've had to move from testing on the live server, to testing locally on a virtual apache server. I've installed XAMPP just fine, downloaded and installed the wordpress files and the database. Everything looks great! The local version of my homepage is identical to the live version. There's only one problem: the homepage is the only page that works. When I click on one of the links i.e. the "about" page (http://localhost/wordpress/about/), I am redirected to the xampp control panel (http://localhost/xampp).
I have a good feeling this has to do with a problem with the "pretty links"/mod_rewrite rules. I made sure I brought over the .htaccess file, and it contains the rewrite instructions. The wordpress database has the proper permalink structure, and the httpd.conf file has the "RewriteEngine on" and the "FollowSymLinks" directives enabled. There has got to be some sort of rewrite problem here, although I am not ruling out something else stupid I might have done. Thanks for all your help!
-E
*Here is what the .htaccess looks like:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
In the httpd.conf, change the
DocumentRoot "/path/to/your/app/wordpress"
also
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/path/to/your/app/wordpress">
This should work, the path is absolute.
And do you have load the:
LoadModule rewrite_module modules/mod_rewrite.so
in httpd.conf??
If your server version works with a domain name, i.e: http://domainname.com is equivalent that http://localhost/wordpress the rewrite rules will be differents. post the rules here.
The problem is that wordpress does not believe in relative paths for some reason. There is an assumption that wordpress is running from the server root not a directory under the root (e.g. /var/www/wordpress will not work, but /var/www/ will).
The problem is with the .htaccess file they provide. It should re-write it to index.php and not /index.php. Change that line in your config and it will work.
What happens is that it tries to actually go to the default document root (in my case /var/www/index.php, which does not exist since I am using http://localhost/worpress which is an alias for ~/projects/worpress). You can check your error log and it will tell you where it is trying to look for the index.php file (which will return a 404 error).
I can go on a rant about how stupid it is that they do that and how bad the whole software design of wordpress is. But I will spare you that :).

.htaccess not working

I have a .htaccess file to remove the index.php part of the codeigniter-style URL. It's been working fine on one computer but when I copied the file over to my laptop it doesn't seem to do anything. I'm using localhost on both machines. They both run mac osx 10.6 with the bundled apache and php and the latest version of mysql. Everything works fine, it's just the .htaccess that doesn't do what it should. Is there any setting that I might have changed on my first machine and forgotten about?!
EDIT:
I'm wondering if there is something wrong with my CI setup now. If I load the base_url ie http://localhost/~User/project/
then it loads perfectly, adding the index.php.
My config file has
$config['index_page'] = '';
However, as a test I returned this value to 'index.php'. When I loaded the base_url after this it returned: http://localhost/~User/project/index.php/index.php/controller/method
Does this suggest anything to anybody?
SOLVED:
I added a new directory command to my httpd.conf file which targeted the specific site I was working on. Seems to be happy now, but not a very satisfactory way of dealing with the problem if I have several codeigniter sites in my web root.
With just a shot in the dark do you have mod_rewrite installed on both?
If your on Ubuntu, you need to edit /etc/apache2/sites-available/default.
sudo vim /etc/apache2/sites-available/default
Where you see lines that say AllowOverride change it to say:
AllowOverride All
You may also need to enable mod rewrite:
sudo a2enmod rewrite
Then, restart the Apache server
sudo /etc/init.d/apache2 restart
Are you sure you have changed the name of the root folder?
Here is what I do:
I put this on my root folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /rootFolder/index.php/$1 [L]
and in the path you replace "rootFoldel" with the name you are using.
hope that helps!
maybe you changed the path relative to the server root. it can breaks .htaccess and may be solved with a RewriteBase /path/to/folder/ statement
edit
since the problem not seems to be this one above, you could also check the error.log file in the apache folder (probably in the logs folder). it may have your answers and it can tell you if the .htaccess is getting parsed wrong or not parsed at all.

Apache site root for development directories on localhost

I think I'm missing something and don't think I really understand how rewriteBase works.
The problem I have is that I have a production site where the site is in the root directory yet I have my development site in a localhost subdirectory. Eg http://www.sitename.com/ vs http://localhost/sitename/
If I have for example an images folder I want to reference the images from the site root by using the initial slash in the href. Eg Using a relative href (without the initial slash) is not an option. This will work on the production site but the development site is looking for http://localhost/images/imagename.jpg instead of http://localhost/sitename/images/imagename.jpg
So I thought all I needed to do was setup the following in my .htaccess file to force the site root to my subdomain within the development environment:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /sitename
But this still uses localhost as the site root instead of localhost/sitename.
Can anyone please give me some pointers?
-------------------------EDIT---------------------------
I stopped trying to do this in the .htaccess file and tried to just use the html command but this also didn't work.
In the end I set up Virtual Hosts in Apache on the local server but it seems like such an awful lot of overkill to just change the site root. I'm also concerned that other developers on the LAN network won't be able to access the site properly via the virtual host.
I'm really needing some 'best practice' advice please on setting up a workable development environment in WAMP.
RewriteBase alone, basically, tells Apache where to apply the RewriteRules. Here you don't have any. By the way, you can either remove the RewriteBase directive altogether, or change it to:
RewriteBase /
The following two lines should get it to work for your development environment only:
RewriteCond %{ REQUEST_FILENAME } !-f
RewriteRule ^(.+)$ /sitename/$1 [L,QSA]
These two directives mean: "if the requested file does not exist (-f), and only in that case, rewrite the url prepending /sitename/ to the requested URI ($1)".
For more info you can have a look at Apache mod_rewrite docs and Apache URL rewriting guide.

What should I use .htaccess for in Zend Framework 1.9.5 application?

I've written several Web MVC apps but never before in PHP, and I'm trying to figure out the best way to work with Zend Framework v1.9.5.
I'm confused about the role of .htaccess in a Zend Framework 1.9.5 application. I have consulted many tutorials, books, and SO questions on Zend Framework (but most of them are for v1.8 at newest) and they all talk about the central role of the .htaccess file. I gather that .htaccess can be used to support virtual hosts, URL rewriting, and to allow Apache to serve static files without going through index.php but I'm not sure if this is current practice or still necessary in v1.9.5.
Currently I have written a couple of pretty simple (HTML, CSS, jQuery) Zend Framework apps and created Apache virtual hosts for them in a test Ubuntu Server 9.10 environment. I didn't use any .htaccess files at all and the apps seem to work fine.
Here's what I did so far:
I created my apps using Eclipse/PDT and the zf.sh tool. I added css, images, and js directories to the public directory that 'zf create project' produced. These apps run fine on my local MAMP installation.
On the server, I installed the Zend Framework in /usr/local/Zend/share/ZendFramework-1.9.5 and added /usr/local/Zend/share/ZendFramework-1.9.5/library to 'include_path' in php.ini.
I copied the apps to the server directories /home/myadmin/public_html/domain[12]/com.
I created virtual hosts by adding entries in the Apache available-sites directory as outlined in Slicehost Virtual Host Setup. I assign DirectoryIndex = index.php and DocumentRoot = /home/myadmin/public_html/domain[12]/com/public. Both apps seem to work fine.
Do I need to use .htaccess files? For what?
*** EDIT - My Solution
Based on Richard's link, I moved the rewrite statements that usually live in .htaccess into my virtual host definition, and now my applications don't use a .htaccess file. My domain1.com virtual host file looks like:
<VirtualHost *:80>
# Define admin email, server name, and any aliases
ServerAdmin webmaster#domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
# Only serve files from inside this directory
DocumentRoot /home/myadmin/public_html/domain1.com/public
# Directly serve any requested files that exist in DocumentRoot;
# otherwise redirect the request to the index.php script
RewriteEngine off
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
</Location>
# Set up log file locations
LogLevel warn
ErrorLog /home/myadmin/public_html/domain1.com/data/logs/error.log
CustomLog /home/myadmin/public_html/domain1.com/data/logs/access.log combined
</VirtualHost>
One primary use of .htaccess for zend framework and most php frameworks is to redirect all requests (except to static files) to a bootstrap file. You don't necessarily need it but your URL would end up looking something like /index.php/controller/action as opposed to /controller/action
You could also just add the rewrite rules to you apache config directly.
The VirtualHost configuration and .htaccess file are two alternatives for URL rewriting in Zend Framework. They perform the same function and you don't need both.
URL rewriting simply directs all requests for non-static files to your index.php.
Routing is the process of decomposing the request URI and figuring out which module/controller/action should be executed. This is a related but separate function to URL rewriting.

Categories