Forum Moderators: phranque

Message Too Old, No Replies

IF filename.php exists, use it; ELSE use filename.html

1960s-era technology makes my head hurt

         

anax

2:54 am on Nov 18, 2005 (gmt 0)

10+ Year Member



OK, I'm in the process of incorporating some PHP features into a previously all-static site. My ultimate objective is to be able to eliminate file extensions in my urls (so I can write mysite.org/topic instead of mysite.org/topic.html). I gather that to do this all I'll have to do is put the line
Options +MultiViews
into the top of my .htaccess file. If it's that easy I don't know why everyone isn't doing it, so that's probably wrong. *But* I haven't gotten that far yet.

Right now I'm stuck with a different problem. Now I've got some files that are filename.html, and some that have been changed to filename.php. What exactly do I need to put in the .htaccess file to insure:

  • if someone requests filename.html and it exists, that file should be used;

  • if someone requests filename.php and it exists, that file should be used; and

  • if someone requests filename.html but it has been replaced by filename.php, then filename.php will be transparently used.

Help will be *very* appreciated!

jdMorgan

3:49 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> if someone requests filename.html but it has been replaced by filename.php, then filename.php will be transparently used.

It's not clear if you intended any kind of precedence in case both a .html and .php file version exist, and to implement such logic would put a burden on your server, but the 'replaced' case can be covered by something like this in .htaccess:


# If html file does not exist, serve php file of same name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ /$1.php [L]

I should also mention that we do not usually allow 'write my code for me' threads here (please see our charter), so the above is meant only to get you started. There are some general set-up issues beyond the specifics of this application that you may need to take care of to use mod_rewrite on your server.

It is occasionally required to add %{DOCUMENT_ROOT} to the filepath in the RewriteCond, depending on the server configuration. That would make it:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f

Try that if the code above seems to rewrite unconditionally.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

anax

4:57 am on Nov 18, 2005 (gmt 0)

10+ Year Member



Great - that seems to work so far. I appreciate your help.

I did spend more than an hour trying to figure this out with material here and elsewhere on the web. The procedures are so completely opaque for anyone who isn't an expert that it is rarely possible to extrapolate from the details of one example to another. For people who have to deal with these problems, say, once a year, it won't ever be realistic to expect a real understanding of the procedures to develop. Stuff like .htaccess is surely responsible for billions of dollars of lost productivity worldwide through bad design - the experts (like you) waste time having to answer similar questions over and over, and the non-experts (like me) who deal with it once or twice a year waste hours fumbling around trying to get it to work. It should have been cast onto the junk pile a generation ago along with DOS and punch cards, but then ... "the future is here, it's just not evenly distributed."