Apache2, php5-fpm - file extension not required - apache

I've set up a server using PHP5-FPM (using PHP 5.3.27) and apache2.
There is an api.php file in the doc root, and using mod_rewrite i've got this rule in .htaccess:
RewriteEngine on
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
So what i want is for everything except exact file matches to be passed to index.php
What is happening, however, is when i go to /api/ the api.php is being loaded, instead of the request being forwarded to the index.php
I've gone through the documentation, googled thoroughly (unless i'm not using the right keywords), and checked out both the apache and php configuration. This doesn't happen on our live server or our development servers that use a conbination of nginx and php5-fpm or apache and mod_php5
My Apache config for the vhost is:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName admin.localhost
ServerAlias *.localhost
<Directory "/var/www/html">
Options FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order deny,allow
Allow from all
</Directory>
# Wire up Apache to use Travis CI's php-fpm.
<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization
</IfModule>
</VirtualHost>
EDIT
I've investigated further:
If i remove api.php - the rewrite works
If i remove the RewriteCond %{REQUEST_FILENAME} !-f - the rewrite works
Any .php file they get loaded without the extension - not non-php files only work with the extension.

Try replacing:
Options FollowSymLinks MultiViews ExecCGI
By:
Options FollowSymLinks -MultiViews ExecCGI
The MultiViews keyword comes from mod_negociation and can have a lot of side-effects. One of theses effect is that it can scan for known extensions, when you request 'api' it can scan for api.php' or 'api.html' files and redirect internally the requests on theses files.
From http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews :
The effect of MultiViews is as follows: if the server receives a
request for /some/dir/foo, if /some/dir has MultiViews enabled, and
/some/dir/foo does not exist, then the server reads the directory
looking for files named foo.*, and effectively fakes up a type map
which names all those files, assigning them the same media types and
content-encodings it would have if the client had asked for one of
them by name. It then chooses the best match to the client's
requirements.
To me this seems like a big hole, some considers it a feature.

Related

Handling '#' in Request URI in Apache Rewrite Rules

It looks like Apache Rewrite Rules are ignoring the '#' in the Request URI.
I want to rewrite the following Request
hptt://localhost/app_name/#Place=abc
to
http://localhost/app_name/#/#ID=abc
And here are the Rules i have tried so far and none of them worked.
RewriteEngine on
# Rule 1
RewriteCond %{HTTP_REFERER} %{HTTP_HOST}/app_name/#Place=(.*)$ [NC]
RewriteRule ^app_name/#Place=(.*)$ /app_name/#/#ID=$1 [R,L]
# Rule 2
RewriteCond %{QUERY_STRING} ^#Place=(.*)$ [NC]
RewriteRule ^app_name/#Place=(.*)$ /app_name/#/#ID=$1 [R,L]
When i tried Rule 1) at http://martinmelin.se/rewrite-rule-tester/ it is working as expected. How ever my local apache is not rewriting the rule
Thanks
Maybe the apache not allowed overwrite rules with .htaccess.
in case you are on ubuntu edit the file /etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
and change it to
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
then restart apache
sudo service apache2 restart
Fragments are not sent in the HTTP Request Messages by web browser. It's pointless to try work with them in an server side.

Laravel Routes not working, Apache configuration only allows for public/index.php/route

Example:
Route::get('/get', function() {
return 'get';
});
To view the route above, I must navigate to public/index.php/get.
I've viewed quite a few SO posts and googled around trying different things and it hasn't made a difference (yes I restart apache every time).
Here is my .htaccess in the public directory:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
What could be causing this still? I'm running Ubuntu.
Two most common causes of this behavior are:
mod_rewrite not enabled
sudo a2enmod rewrite && sudo service apache2 restart
AllowOverride is set to None, set it to All, assuming Apache2.4
sudo nano /etc/apache2/apache2.conf
search for <Directory /var/www/> and change AllowOverride None to AllowOverride All, then save the file and restart apache
Do you have mod_rewrite installed and enabled on apache? Try to remove the if lines ( and ) and see if it throws an error when you try to load the website. If it does, run sudo a2enmod rewrite and restart apache.
This is the .htaccess I have on my public/ directory:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
In the Apache Configuration file may be located at /etc/httpd/conf/httpd.conf (location and names varies on server), provide AllowOverride permission for .htaccess file by updating the directory as follows:
<Directory "/var/www/laravel-sample-project/public">
AllowOverride All
</Directory>
This may solve your problem on laravel routes.
Also ensure that you're providing access "AllowOverride All" limited to directories, such as mentioned above and limited to the directory /var/www/laravel-sample-project/public (mentioned here is the root directory for my project which may varies for you)
Changing AllowOverride None to AllowOverride All and sudo a2enmod rewrite && sudo /etc/init.d/apache2 reload did help.
I know it's an old question, but for people coming from search engines:
Although the apache site config I had enabled (/etc/apache2/sites-enabled/sitename.conf) had AllowOverride All
But for some reason the /etc/apache2/apache2.conf file had AllowOverride None for /var/www/ directory.
Solution:
Run nano /etc/apache2/apache2.conf
Find Directory /var/www/ (on mac you can ctrl+w, paste the text and hit enter)
Inside it, change AllowOverride None to AllowOverride All
So, this
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
becomes this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
If you do not use a .htaccess file, you need to specify the directory for your redirections:
<IfModule mod_rewrite.c>
RewriteEngine On
<Directory /home/vitela/laravel/public>
RewriteBase /public
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L,QSA]
</Directory>
</IfModule>
In MacOs. The Main problem of .htaccess not working is there is not mod_rewrite.so enabled in httpd.conf file of apache configuration.
i have solved this by uncomment the line :
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Remove the # from above line httpdf.conf. Then it will works. enjoy!

Turning RewriteEngine on creates 403 error--how to turn on FollowSymLinks?

I am working with the built-in Apache2 on OSX. I moved the document root to a folder on my desktop and made sure that _www and everyone have read access. It works fine and great, PHP works, everything works until I add a .htaccess with this line only:
RewriteEngine on
As soon as I do that, everything in the directory with the file is 403 Forbidden:
Forbidden
You don't have permission to access /dir/file.txt on this server.
Apache logs show this error:
[error] [client 127.0.0.1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /Users/uname/Desktop/localhost/dir/filename.txt
I've gone through httpd.conf and made sure that I've enabled FollowSymLinks to no avail:
DocumentRoot "/Users/uname/Desktop/localhost"
<Directory />
Options FollowSymLinks
Options SymLinksIfOwnerMatch
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/Users/uname/Desktop/localhost">
Options FollowSymLinks
Options SymLinksIfOwnerMatch
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
If I comment out RewriteEngine on in the .htaccess file, then everything works fine, except rewrite rules, obviously. On OSX 10.8.5, httpd.conf is located in /etc/apache2 which also has the folder users containing individual files for uses, e.g. uname.conf, but I've added symlinks in here same as in the other. I noticed there's also a folder extra with files like httpd-userdir.conf, but they don't seem to disable anything.
Where else can FollowSymLinks be turned on (or off)?
You have to either put your Options on one line, or add a + sign in front of your Options so Apache understands you want merge them. At the moment only the last Options directive ('Options Indexes MultiViews') is being applied since it is overwriting all the prior Options.
Try this (which will overwrite the '/' Options):
<Directory "/Users/uname/Desktop/localhost">
Options Indexes MultiViews FollowSymLinks SymLinksIfOwnerMatch
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I had an issue with getting a 403 the solution for me was changing my rewrite rules. My complete directives are as follows:
<Directory "/var/www/">
<LimitExcept GET POST HEAD>
Order Allow,Deny
Deny from all
Satisfy all
</LimitExcept>
Require all granted
AllowOverride None
# Disable includes and cgi
Options -Includes
Options -ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirdect to HTTPS
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule ^(.*)$ - [F,L]
# Put your installation directory here:
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# remove index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
# If your host requires forcing query strings.
# Notice the question at the end of index.php
# on the last rule
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
</Directory>
ServerName www.example.com
ServerAlias www.example.com

Codeigniter controller in subdirectory not working in virtual host

I need help with CodeIgniter -- trying to it to work in virtual host envirnoment in Ampps.
I have been following the following tutorial:
http://net.tutsplus.com/tutorials/php/basecamp-style-subdomains-with-codeigniter/
I am trying to have two installations of CodeIgniter running in two directories:
/students and
/teachers
www.test.com -> goes to /teachers directory
www.user1.test.com -> goes to /students directory
I have the above working on my local machine. The different URL's take me to the correct directory. CI works. Problem arises when I try to access controller using redirect.
if I try to redirect('error') I get:
user2.test.com/user2.test.com/error
Also get an "internal server error"
if I type in
user2.test.com/error
I get an "internal server error"
if I type in
user2.test.com/index.php/error
I get desired result
It works if I try to load view directly in
$this->load->view('error')
I think I need help with the urls and paths. Any guidance most gratefully received.
Nothing particularly enlightening in CI log files
In Apache logs I find:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Things I have changed:
htaccess in root - changed rewrite base. Also takes out index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /students/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
application/config.php
$config['base_url'] = $_SERVER['HTTP_HOST'];
As per tutorial. Needs to be dynamic.
my Apache hppd-vhosts.conf file
NameVirtualHost 127.0.0.1:80
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:/Program Files/Ampps/www/teachers"
ServerName test.com
ServerAlias test.com
<Directory "C:/Program Files/Ampps/www/teachers">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/Program Files/Ampps/www/students"
ServerName test.com
ServerAlias *.test.com
<Directory "C:/Program Files/Ampps/www/students">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
index.php in root
kept as is. Things got worse when I tried to change these
$system_path = 'system';
$application_folder = 'application';
what about your host file? your host file should be 127.0.0.1 test.com and first try to disable the student sub domain and see if it dose work. Its look like your both ServerName test.com is same try to change the other one with student.com and see whats happen most probably both are conflicting

403 forbidden using fastcgi

Trying to set up a ReviewBoard server, using apache 2.2 with fastcgi on Windows 7 (no, Linux isn't an option). I'm using (basically) the default httpd.conf, with mod_fcgid and mod_rewrite loaded, and
Include C:\mars\reviews\conf\apache-fastcgi.conf added at the bottom. This too is the default, excepting the Order, Allow, and +Indexes. The trouble is, when I attempt to load the site, I get 403 for / (which, according to the rewrite rule, would become reviewboard.fcgi). I added +Indexes to prove that yes, in fact, I can access those directories, so it doesn`t appear to be a permissions problem.
When I add ExecCgi, those 403 errors turn into 404 errors! I'm at my wits end, and over my head.
The contents of C:\mars\reviews\conf\apache-fastcgi.conf (with ServerName redacted):
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
</IfModule>
<IfModule mod_fastcgi.c>
AddHandler fastcgi-script .fcgi
FastCGIServer "c:/mars/reviews/htdocs/reviewboard.fcgi" -socket "c:/mars/reviews/tmp/fastcgi.sock"
</IfModule>
<VirtualHost *:80>
ServerName #redacted
DocumentRoot "c:/mars/reviews/htdocs"
# Alias static media requests to filesystem
Alias /media "c:/mars/reviews/htdocs/media"
Alias /errordocs "c:/mars/reviews/htdocs/errordocs"
# Error handlers
ErrorDocument 500 /errordocs/500.html
<Directory "c:/mars/reviews/htdocs">
Options +Indexes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# Direct all other requests to the fastcgi server
RewriteEngine on
<IfModule mod_fcgid.c>
RewriteRule ^/(media.*)$ /$1 [QSA,L,PT]
RewriteRule ^/(errordocs.*)$ /$1 [QSA,L,PT]
</IfModule>
<IfModule mod_fastcgi.c>
RewriteRule ^/(media.*)$ /$1 [QSA,L,PT]
RewriteRule ^/(errordocs.*)$ /$1 [QSA,L,PT]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ /reviewboard.fcgi/$1 [QSA,L]
</VirtualHost>
What on Earth is going wrong here??
It seems to me, that you have mod_fcgid installed, but FastCGIServer-statement is in mod_fastcgi-section. I'd started here.
After speaking with Christian Hammond at ReviewBoard, he pointed me to this django ticket. It turns out that django doesn't support FastCGI on Windows!