What exactly does the Multiviews options in .htaccess? - .htaccess

I've been struggling a lot with an access rule that needed to rewrite one piece of URL adding a path.
RewriteRule ^(configuration/.+)$ application-server/$1 [L,NC,R=301,NE]
This Rule caused just a blank page on my Joomla site with no error log or messages.
The curious thing is that all other rules I had worked perfectly:
RewriteRule ^(log/.+)$ application-server/$1 [L,NC,R=301,NE]
RewriteRule ^(monitor/.+)$ application-server/$1 [L,NC,R=301,NE]
in the end, I've found in a forum a suggestion to use the following option:
Options -Multiviews
That actually solved the issue, however I wonder if there can be any side effects on other Rules when using this option.

This is about Apache content negotiation.
A MultiViews search is where the server does an implicit filename pattern match, and choose from amongst the results.
For example, if you have a file called configuration.php (or other extension) in root folder and you set up a rule in your htaccess for a virtual folder called configuration/ then you'll have a problem with your rule because the server will choose configuration.php automatically (if MultiViews is enabled, which is the case most of the time).
If you want to disable that behaviour, you simply have to add this in your htaccess
Options -MultiViews
This way, your rule will be now evaluated because content negotiation is disabled.
Edit
On some shared hostings, the negotiation module might not be enabled. That would give you then a 500 error. To avoid this error, you can, by default, encapsulate the directive in an IfModule block.
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

Related

Why my rewrite rule in htaccess is working in some case only?

I try to rewrite some of my URLs with a .htaccess file but it didn't work as expected.
This is the rewrite rule in my .htaccess file :
RewriteRule ^(index|administration)/([A-Za-z0-9-]+)(\.php)?$ index.php?c=$1&t=$2 [QSA]
When I go on www.example.com/index/main, I get a 404 error code.
So I try to change my rewrite rule to
RewriteRule ^index.php$ index.php?c=index&t=main [QSA]
Then I go to www.example.com/index.php and the webpage displays perfectly with all the datas in $_GET (c = index and t = main).
So I don't know why my first rule is not working. Let me see if you have any idea.
Is it possible that my server wants to enter the index folder, then the main folder for my first rule without taking care of my .htaccess (www.example.com/index/main) ?
You need to ensure that MultiViews (part of mod_negotiation) is disabled for this to work correctly. So, add the following at top of your .htaccess file:
Options -MultiViews
If MultiViews is enabled (it's disabled by default, but some hosts do sometimes enable this in the server config) then when you request /index/main where /index.php already exists as a physical file then mod_negotiation will make an internal request for index.php before mod_rewrite is able to process the request. (If index.html also exists, then this might be found first.)
(MultiViews essentially enables extensionless URLs by mocking up type maps and searching for files in the directory - with the same basename - that would return a response with an appropriate mime-type.)
If this happens then your mod-rewrite directive is essentially ignored (the pattern does not match, since it would need to check for index.php) and index.php is called without the URL parameters that your mod_rewrite directive would otherwise append.
it perfectly works by disabling the MultiViews Option in my .htaccess
This would ordinarily imply its your script (ie. index.php) that is triggering the 404 (perhaps due to missing URL parameters?), rather than Apache itself?
However, if you were seeing an Apache generated 404 then it would suggest either:
You also have an index.html file, which is found before index.php. .html files do not ordinarily accept path-info (ie. /main) so would trigger a 404.
OR, AcceptPathInfo Off is explicitly set elsewhere in the config, which would trigger a 404 when the request is internally rewritten to /index.php/main (by mod_negotiation).

Why does this RewriteRule work for all extensions but .php?

This simple RewriteRule that I am using for practicing with .htaccess files works almost always:
RewriteEngine on
RewriteRule ^.*$ test.html
When I have the file flowers.html and I use http://localhost/flowers I get redirected to test.html, however when I rename flowers.html to flowers.php I get a 404 page with the message The requested URL /flowers was not found on this server. Does anyone know what causes this?
EDIT:
When I create an empty file called flowers it does redirect properly to test.html. What is going on here?
This does sound like a conflict with MultiViews, so try adding the following at the top of your .htaccess file to disable MultiViews:
Options -MultiViews
MultiViews is not enabled by default, so maybe this has been enabled in your server config?
When MultiViews (part of mod_negotiation) is enabled, a request for /flowers (no extension) will result in Apache searching for an appropriate file to return (based on mime-type) by trying various file extensions of files found in that directory. This is achieved with an internal subrequest before mod_rewrite runs.
However, it's not clear why this would be a problem in your case if you have no other directives? Since your directive simply rewrites everything to test.html (which should include any subrequests). (I was unable to reproduce this behaviour on my Apache 2.4 test server - hence my initial doubt.)

rewrite index.php?p=page to page

My urls currently look like this: http://www.morrisononline.com/index.php?p=contact
I would like to rewrite them to http://www.morrisononline.com/contact
The content for (in this case) contact comes from the directory /pages/ (www.morrisononline.com/pages/contact.php)
I have tried the following in the .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?p=$1 [NC,L]
I have also tried other combinations too (I'm not very good at this and trying lots of suggestions from other forum posts etc) but nothing has any effect. No error messages and no rewrites either. Did I do it wrong or could there be another command before that, that overrides this?
Do I have to refresh some cache or restart the server for this to work?
Make sure you have AllowOverride All or at least AllowOverride FileInfo in your site's config for the site's document root. And make sure the htaccess file is in the document root, is readable by everyone. If it's still not working, make sure that you've got AccessFileName set to .htaccess.

htaccess rewrite working offline on WAMP but not online on linux host

I'm just going to explain my problem here :
http://mysite.com/movie/20000
should be rewritten to
http://mysite.com/movie.php?id=20000
my htaccess file :
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie/([0-9]+)$ movie.php?id=$1
On my localhost WAMP installation this works fine, but when I put it online on my linux host it doesn't completely work. It does go to the movie.php page, but it seems it gives no GET parameter id.
Edit :
if I use
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie([0-9]+)$ movie.php?id=$1
then
http://mysite.com/movie20000
Goes to the correct page, but I would like /movie/20000 and not just /movie20000
It also seems that my host automatically rewrites a visit to mysite.com/movie to mysite.com/movie.php
After searching for a long time, and pulling some of my lovely hair out I found the solution.
I just added
Options -MultiViews
to my htaccess file and that fixed it.
Why? Because .php was being added to urls without an extension, which I really did not need.
This should work.
RewriteRule ^movie/([0-9]+)$ http://mysite.com/movie.php?id=$1 [NC,L]
Don't forget the [NC, L] it means Case insensitive, last rule... If you don't, it will continue to process through your htaccess rules, maybe triggering other ones.
Still, the stuff below is good advice.... :)
Check to see if the Rewrite module is loading with apache. Look for this line in the httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Check to see if your apache config allows for .htaccess files for your system or in the virtual host definition.
Also, test with a simpler rewrite catch all and test that alone to see if it's working at all like this (get rid of everything else in your htaccess file to limit the test):
RewriteEngine On
RewriteRule ^(.*)$ http://www.google.com [L,R=301]
Any request to your site should go to google if the configuration for apache is correctly set.

Apache mod_rewrite works in .htaccess but not httpd.conf

I'm experimenting with mod_rewrite for the first time (I'm a web newbie, but trying to learn). I'm trying to get bob.html to redirect to alice.html (read: URL stays the same, page content is alice.html). Both files are in /var/www/. I'm running Ubuntu 10.10 w/Apache 2.2.16.
Here's what works in the .htaccess file placed in the /var/www/ directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bob.html$ alice.html
</IfModule>
This behaves as expected, but it does not work when moved to the httpd.conf file (just learning best practices w/performance and such). I read somewhere that when using mod_rewrite in httpd.conf file, leading slashes are required, so my httpd.conf file looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/bob.html$ /alice.html
</IfModule>
I also read that /etc/apache2/sites-enabled/000-default needs to have AllowOverride set to All.
<Directory /var/www>
...
AllowOverride All
...
</Directory>
I think that's just for use with .htaccess but I wasn't sure so I just left it in.
With all of these configuration settings, the redirection does not work. I've tested to make sure the file itself is being read (someone suggested httpd.conf isn't used anymore and apache2.conf is used instead) by inserting erroneous code. I've taken a look at a couple other questions/answers but I still cant figure it out.
Edit: It should be noted that I'm using /etc/init.d/apache2 restart after each change to httpd.conf to restart Apache and (hopefully) reload the configuration.
Try wrapping the rewriterules in your httpd.conf file in <Location /></Location> tags. The seems to alter the behavior making it more similar to the .htaccess file.

Resources