Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Use of REQUEST_FILENAME in Apache mod rewrite

[Aug 22, 2012] Apache2 mod_rewrite and %{REQUEST_FILENAME} Sysadmandine

23-02-2010

I’m trying to develop a new website to increase my php object oriented skills. For this new website, I want every request for any url that doesn’t match a actual file on the disk to be redirected to index.php (to handle parameters in fact). Easy with apache2 rewrite rules :

RewriteCond %{REQUEST_FILENAME} 	!-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^/(.*)$ /index.php?rt=$1 [L,QSA]

This means : if the requested file is not a real file, and isn’t a directory, and isn’t a symlink, then redirect to index.php.

I was really surprised to discover that it doesn’t work. Though, everybody seems to use this syntax ! I checked my apache version : Apache/2.2.9 (Debian), nothing special with this one I guess.
To understand what Apache was doing with my rewrites, I activated the rewrite log :

RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 5


Here’s what I got (the interesting part, cause I got a looot more !) :

[blah blah blah] (2) init rewrite engine with requested uri /toto.htm
[blah blah blah] (3) applying pattern '^/(.*)$' to uri '/toto.htm'
[blah blah blah] (4) RewriteCond: input='/toto.htm' pattern='!-f' => matched
[blah blah blah] (4) RewriteCond: input='/toto.htm' pattern='!-d' => matched
[blah blah blah] (4) RewriteCond: input='/toto.htm' pattern='!-l' => matched
[blah blah blah] (2) rewrite '/toto.htm' -> '/index.php?rt=toto.htm'

So apaches verifies only ‘/toto.htm’ and not the whole path for “%{REQUEST_FILENAME}”? I thought though it was the whole path… let’s verify in the doc.
From http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html, by habit (cause I used apache 2.0 a lot more than apache 2.2 from now on) :

REQUEST_FILENAME : The full local filesystem path to the file or script matching the request.

Hmm. But I use apache version 2.2, so what do they say here http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html :

REQUEST_FILENAME : The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.

Ow.

REQUEST_URI : The resource requested in the HTTP request line. (In the example above, this would be “/index.html”.)

Ok, I understand, I use virtual hosts (like everybody, uh?), so the real syntax for my needs is :

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} 	!-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l RewriteRule ^/(.*)$ /index.php?rt=$1 [L,QSA]

This works even if it doubles the “/” between each variable (one / at the end of DOCUMENT_ROOT, and another at the beginning of REQUEST_FILENAME).

Here’s the rewrite log showing that it works :

[blah blah blah] (2) init rewrite engine with requested uri /toto.htm
[blah blah blah] (3) applying pattern '^/(.*)$' to uri '/toto.htm'
[blah blah blah] (4) RewriteCond: input='/path/to/documentroot//toto.htm' pattern='!-f' => not-matched
[blah blah blah] (1) pass through /toto.htm

Now I can disable this log if I want to keep space on my disk.

I must admit I read the description for REQUEST_FILENAME in apache2.2 several times before noticing that it was just the answer… too used to read too fast! Thanks to this old post that made me re-read slower ! ;)

Similar Posts:

TarK` says...

Haha.
Rewrite rules are magical, but can quickly become as complicated as hell.

See you soon in the train… Some day?

Alex from Maizières ;)

Posted on: 5/Aug/2010@16:07

Parhs says...

Thank you man..
I lost 4 hours of my day to solve these problems….
Stupid mod_rewrite…..

What’s up with REQUEST_FILENAME in mod_rewrite- « Marc …

According to the mod_rewrite docs, REQUEST_FILENAME is “The full local filesystem path to the file or script matching the request.”

If that were true, this idiom, which I’ve seen in a number of places,
should work to prevent the RewriteRule underneath it from firing when a request is made for a file which exists in the filesystem:

RewriteCond %{REQUEST_FILENAME} !-f

But it doesn’t. Looking at the rewrite log reveals that REQUEST_FILENAME, on my system at least, is not the full filesystem path; it is actually relative to the document root. Which means that the condition is never true and the rewrite will always happen.

So in order to get it to work, I have to do this ugly hack:

RewriteCond /home/y/share/htdocs%{REQUEST_FILENAME} !-f

What does this mean RewriteCond %{REQUEST_FILENAME} !-f-d [Archive] - Mambo - Forums Closed for posting

What does this mean: RewriteCond %{REQUEST_FILENAME} !-f/d ?


QReyes

May 3rd, 2005, 04:16

What does this mean:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

I found this in the .htaccess file of Mambo.

All I know is that if you use RewriteRule ^(.*) index.php, it will redirect everything entered from http://www.sample-domain.com to index.php so that you can parse the URI. But what about the rest of the code?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

What do they do?


RobInk

May 3rd, 2005, 05:51

Hi,

Found this on Google, maybe some explanation to your question:

RewriteEngine on
This line starts the mod_rewrite module
RewriteCond %{REQUEST_FILENAME} !-d
If the request is for a real directory (one that exists on the server), index.php isn't served.
RewriteCond %{REQUEST_FILENAME} !-f
If the request is for a file that exists already on the server, index.php isn't served.
RewriteRule ^(.*)$ /index.php
</IfModule>All other requests are sent to index.php. </IfModule> is a closing tag for the rewrite module.


QReyes

May 3rd, 2005, 19:04

That was really helpful. Thanks. :) What about this one:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %{REQUEST_FILENAME} [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ %{REQUEST_FILENAME}/index.php [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^ 404/ [L]

RewriteRule ^(.*[^/]) index.php?var=$1 [QSA,L]

What does it mean? And which one is better?


RobInk

May 3rd, 2005, 22:58

Hi,

You might want to read the following page(s).
Thats explains it all, gives you a better understanding on mod_rewrite.

http://httpd.apache.org/docs/mod/mod_rewrite.html


QReyes

May 3rd, 2005, 23:42

No explanation about -f or -d there.
RobInk

May 4th, 2005, 00:20

Yes there is ;-)

Scroll down,choose RewriteCond or RewriteRule link, you get :

http://httpd.apache.org/docs/mod/mod_rewrite.html#RewriteCond

Scroll down, half way, there parameters are discussed, again down, examples ;-)

So on the main link, a little more below, you find all the commands for mod rewrite, in the table of contents there, these are links to seperate pages ;-)


QReyes

May 4th, 2005, 01:38

Thanks, man. Here's a follow-up.

Someone from the other board said that I should use:

ErrorDocument 404 /index.php

Instead of the code I've posted above since it is an overkill. If using ErrorDocument is much better, then why does Mambo uses the code below:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RobInk

May 4th, 2005, 01:42

Hi,


To be honest, never looked into the code in the ambo .htaccess. All i know is that i need it for the SEO/SEF to work. So i think its for the SEF to work, all i need to know ;)


QReyes

May 4th, 2005, 01:57

But the code:

ErrorDocument 404 /index.php

will also work for SEF. So why not use that?


thede

May 4th, 2005, 02:11

I know that I sometimes suggest to use ErrorDocument when mod_rewrite doesn't work...
But I don't know which one is faster... this site says, that ErrorDocument is faster:
http://www.apachefreaks.com/apache/misc/rewriteguide.html
But you could try to search the web for more evidence.

I believe Mambo uses it because they were not aware that ErrorDocument works as well or even more often. That mod_rewrite is required is an incomplete statement. You could use whatever you want to redirect to the index.php if the file/directory does not exists (of course the original request-url need to be still available). Even if you are not allowed to set an ErrorDocument in your .htaccess you could have a setting for this in your control panel. If you are using an IIS for example, there is no .htaccess and it could still work. You might need some little tweaking if the original URL is somewhere else (not in REQUEST_URI).

As you recognized by yourself now... the default Mambo rule is kind of exactly what ErrorDocument is... so use what you prefer (I use ErrorDocument because I had problems with mod_rewrite on my host and did not bother to find out whether they could be solved).

EDIT: I may add that ErrorDocument refers here to 404 only.


RobInk

May 4th, 2005, 02:19

This sure clears things up, thanks Thede !
thede

May 4th, 2005, 03:18

I thank you for your help here... if you continue like this you will certainly get your 6th star first :-)
RobInk

May 4th, 2005, 03:26

I'm not in a race :D Just like to help out a bit... and yes, getting close to that last flower...

If they would form another team, say a support team for mambo, i would sure volunteer ;-)


QReyes

May 4th, 2005, 03:29

If I use ErrorDocument, would it clog the error logs? And wouldn't search engines reject it since they'll know it is an error?

OT: Is there a team already? I read something about this in the annoucement not long back.


RobInk

May 4th, 2005, 03:31

Hi,

As far as i know three teams:

- development
- maintenance
- documantation


QReyes

May 4th, 2005, 04:01

Back to the topic:

If I use ErrorDocument, would it clog the error logs? And wouldn't search engines reject it since they'll know it is an error?


thede

May 4th, 2005, 04:06

Back to the topic:
Not giving up, eh? ;-)

Well, I think 404 errors do not get logged in the error.log... the error log contains 5xx errors (so real errors)...
You have a access.log which usually contains the request path, the status code and some other useful information.
But a page is able to set by itself which status code to send (200, 404, ...)

Conclusion: you should not have more logs than usual


QReyes

May 4th, 2005, 04:25

Someone from the PHP mailing list said I should use header ("HTTP/1.0 200 OK");
thede

May 4th, 2005, 04:28

Someone from the PHP mailing list said I should use header ("HTTP/1.0 200 OK");
Yes, thats how a script can set the status code to 200...
But I am not sure whether Mambo does this already, but you could always add this if it is not there. Just check your access log and you will see what is returned in the end ;-)
QReyes

May 4th, 2005, 04:34

I don't think they need to use it since they're not using the ErrorDocument directive.
thede

May 4th, 2005, 09:26

I don't think they need to use it since they're not using the ErrorDocument directive.
You never know what they implement just in case ;-)
However, the SEF component (404 SEF) which I am using does change the status code that way.

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_F - Pastebin.com

  1. RewriteEngine On
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteRule ^(?<!\?)(.*)$ /index.php?$1 [L,NS]
  5. RewriteCond %{QUERY_STRING} ^(?!search)(.+)$
  6. RewriteRule ^$ /%1? [R=301,L]
  7. RewriteCond %{HTTP_HOST} !^(dev|www|www1|www2|www3|fr|de|es|ru).wowhead.com$
  8. RewriteRule ^$ http://www.wowhead.com/$1 [R=301]
  9. RewriteRule ^index.php http://www.wowhead.com/ [R=301]
  10. # static content redirects
  11. RewriteRule ^ads(.*) http://static.wowhead.com/ads$1 [R=301]
  12. RewriteRule ^css(.*) http://static.wowhead.com/css$1 [R=301]
  13. RewriteRule ^images(.*) http://static.wowhead.com/images$1 [R=301]
  14. RewriteRule ^js(.*) http://static.wowhead.com/js$1 [R=301]
  15. RewriteRule ^widgets(.*) http://static.wowhead.com/widgets$1 [R=301]
  16. Redirect 301 /eyewonder/interim.html http://static.wowhead.com/ads/eyewonder/interim.html
  17. Redirect 301 /eyeblaster/addineyeV2.html http://static.wowhead.com/ads/eyeblaster/addineyeV2.html
  18. Redirect 301 /doubleclick/DARTIframe.html http://static.wowhead.com/ads/doubleclick/DARTIframe.html
  19. Redirect 301 /PointRollAds.htm http://static.wowhead.com/ads/PointRollAds.htm
  20. Redirect 301 /videoegg/ad.html http://static.wowhead.com/ads/videoegg/ad.html
  21. Redirect 301 /trafficmp/eyengage.html http://static.wowhead.com/ads/trafficmp/eyengage.html
  22. RewriteRule \.svn/ http://www.wowhead.com [R=404,L]
  23. RewriteCond %{QUERY_STRING} ^wotlkguide$ [or]
  24. RewriteCond %{QUERY_STRING} ^wotlkguide=$
  25. RewriteRule ^$ http://www.wowhead.com/?guide=wotlk [L]
  26. RewriteCond %{QUERY_STRING} ^wotlkguide=(.*)$
  27. RewriteRule ^$ http://www.wowhead.com/?guide=wotlk&%1 [R=301]



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019