Apache ignore *.php files in rewrite directives but not other extensions - apache

I'm trying to test some simple rewrite directives in .htaccess file on my test environment with Apache and PHP-FPM.
It seems that a simple rewrite rule works well for any type of file but not for *.php files.
This works :
RewriteEngine On
RewriteRule ^sandbox/video\.html?$ http://www.google.fr? [R,L]
But this not :
RewriteEngine On
RewriteRule ^sandbox/video\.php?$ http://www.google.fr? [R,L]
I run Apache 2.2 and PHP-FPM inside a VM on Debian
Could it be related ?
Thanks for your help

With PHP-FPM the .htaccess file is never read for php URL. You will need to add the rewrites to your main config.

Related

hhvm fastcgi and apache .htaccess rules

Ok so I have this rewrite rule:
RewriteRule ^images/$ index.php?id=images [L]
So hhvm instead of using this rule it searches first for the images folder and looks for a index.php file
I'm using this proxy directive in the virtual host file:
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/hhvm/mydomain/htdocs/$1
What can be done so this rule can work correctly even if the folder images exists ?
Thanks,

rewrite rules not working

I use some rewrite rules inside the Apache virtual hosts configuration httpd-vhosts.conf and it works fine there. Because I want to move to a managed server I have to use the .htaccess file because I don't have access to the apache configuration files.
I tried it locally and somehow I don't get this working at all.
Locally I use xampp 1.8.3 (Apache 2.4.10). I put the .htaccess file in the root directory of the virtual host and use this lines for testing
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /testrule /about/about.php [L]
I always get an 404 error when calling localhost/testrule
It works fine when I put it in the httpd-vhosts.conf.
mod_rewrite is enabled and I have set AllowOverride All in the Directory part.
This blog post clearly mention
In VirtualHost context, The Pattern will initially be matched against
the part of the URL after the hostname and port, and before the query
string (e.g. “/app1/index.html”).
In Directory and htaccess context, the Pattern will initially be
matched against the filesystem path, after removing the prefix that
led the server to the current RewriteRule (e.g. “app1/index.html” or
“index.html” depending on where the directives are defined).
So in virtual host context, matches start from /testrule/...
While in .htaccess and <Directory/> context, it matches testrule/...
You can quick check the htaccess rewrite rules here:
http://htaccess.madewithlove.be/ or here:
http://martinmelin.se/rewrite-rule-tester/
Just get rid of the leading slash in the RewriteRule and you are done
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule testrule /about/about.php [L]

.htaccess mod_rewrite dynamic match with Yii generated urls

I'm trying to use Apache's mod_rewrite module to make dynamically generated urls from a Yii app, which always contain index.php as the router/bootstrap, into static urls through .htaccess available to browse through a web browser.
Example: localhost/projectname/index.php/commentfeed.xml is the dynamically generated page, but I want it to be able to be accessed through localhost/projectname/commentfeed.xml
Apache version I'm running is: Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.0 through XAMPP for my development platform. The mod_rewrite module is present and loaded in Apache's httpd.conf file.
My .htaccess in localhost/projectname/ is as follows:
#Turning on the rewrite engine is necessary for the following rules and features.
#FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.so>
Options +FollowSymlinks
RewriteEngine On
#Unless an explicit file or directory exists,
#redirect all request to Yii entry script.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Now, when I try to access commentfeed.xml through my browser without index.php (localhost/projectname/commentfeed.xml) I get a 404 error - Object not found!.
I've looked over the following guides & SO questions: URL Rewriting Guide, mod_rewrite documentation, and SO: Tips for debugging .htaccess rewrite rules but none of them had explicitly what I was looking for. Also, my apache error log is not reporting anything about the .htaccess file. Any help or guidance towards the correct usage would be greatly appreciated.
You need to tell the Yii UrlManager that the script name is not required. In your config/main.php file you should have:
'components'=>array(
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
),
),
),
Make sure you also have a route defined to specify the controller/action that generates the commentfeed.xml file.
More info: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
The specific answer to my problem lay in using <IfModule> ... </IfModule>. With my setup of XAMPP v3.2.1 and the apache server installed with it, the only lines of code I needed in the .htaccess was simply:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

Moving Joomla SEF rules from .htaccess to vhosts.conf

I have a joomla install which uses the built in SEF urls, and the stock .htaccess file to rewrite them.
I presume that it would be much more efficient to set the rules in the apache config rather than .htaccess, [to avoid having this file requested and parsed for every file load] but am having trouble finding a reference for it,
So I moved the rules from .htaccess to the vhosts.conf file.
I am now getting 400 bad requests for the pages with the SEF urls.
Can anyone suggest what the issue or offer any suggestions ?
UPDATE
It seem that the base was the issue, just need to change the rewrite rule from:
RewriteRule .* index.php [L]
to
RewriteRule .* /index.php [L]
where in the vhosts.conf file have you placed these rules? they should be in the
<Directory /path/to/joomla>
#your rules here
</Directory>
inside the Vhost definition

htaccess rewrite issue while testing on localhost

I'm running OSX (10.6) with Apache with .htaccess enabled.
In the htaccess file I have the code:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/([A-Z0-9._%+-]+) index.php?page=$1 [NC]
This runs perfectly on my external server and redirects nicely to index.php?page=whatever
However when testing locally from localhost/~james/In%20Progress/Vila%20Maninga/page/whatever I get redirected to localhost/~james/Users/James/Sites/In%20Progress/Vila%20Maninga/index.php?page=whatever. For some reason 'Users/James/Sites/' is being added.
Does anyone why this is happening and how to prevent it?
Many thanks,
James
Not 100% sure this is the problem, but try adding this to your .htaccess file
RewriteBase /
(substitute / with whatever path you like)