if you have a folder where you have .html files,
in your apache settings you can tell the server to parse .html fiels as .php
you can read a valuable conversation about this theme:
Showing a new logo each day - getting it to work?
Here is an example from one of my .htaccess files. First I physically converted all my .htm and .html extensions to .php. Then, this code in my .htaccess will take any request for an .htm or .html file and redirect it to the same file name but with a .php extension:
Options +FollowSymlinks
RewriteEngine on
RedirectMatch permanent ^(.*)\.htm(l?)$ $1.php
Sometimes you might have certain subdirectories in your site were you don't want the php redirect to happen. You can put a new .htaccess in the subdirectory to turn off the redirect just for that directory. The code is a little bit different, but it is explained here:
http://corz.org/serv/tricks/htaccess2.php?page=all
Another way of doing this worth consideration is to make a header.php file, something along these lines:
<?PHP
$date = date('Y-m-d');
if(file_exists('grafix/ap_logo_'.$date.'.gif')) {
$file = 'grafix/ap_logo_'.$date.'.gif';
}
else {
$file = 'grafix/ap_logo_standard.gif';
}
header('Content-Type: image');
readfile($file);
?>
Then you could do:
<img src="header.php" alt="header image" />
In all your static HTML pages, and the PHP page would figure out which graphic to parse and display. This would allow you to prevent passing all your pages through the PHP interpreter when you just need to figure out which image to display.
I've not tested your specific implementation, but I'm using a similar setup elsewhere to load a random image through PHP.
On a related note, you could also do this with JavaScript and not need to convert your .html pages to .php, but would then be dependent on your users having JavaScript enabled.
Edit: Another note, the following code should be functionally equivalent to my solution with your code and readfile(). You could further reduce it by removing the $date_image variable and doing the date() check inside the file_exists() check inside readfile(), but that felt messier than the initial solution.
<?PHP
$date_image = 'grafix/ap_logo'.date('Y-m-d').'.gif';
header('Content-Type: image');
readfile(file_exists($date_image) ? $date_image : 'grafix/ap_logo_standard.gif');
?>
Thak you Horizon88, sounds good not to have to parse all pages... so to summarize, this is what i should do?
<?PHP
$date_image = 'grafix/ap_logo'.date('Y-m-d').'.gif';
header('Content-Type: image');
readfile(file_exists($date_image) ? $date_image : 'grafix/ap_logo_standard.gif');
?>
<img src="header.php" alt="header image" />
The .htaccess file:
I would be greatful for some help with a .htaccess file if there is anyone out there who knows this stuff!
My site is coded in .html (Transitional XHTML 1.0)
It uses includes for the header and footer, they look like this:
<!--#include virtual="/header.txt" -->
I want to add some php to the header file that changes the logo each day.
My .htaccess file looks like this:
RewriteEngine on
Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AddType application/x-httpd-php .php .html
RewriteCond %{HTTP_HOST} alternativephotography.com$ [OR]
RewriteCond %{HTTP_HOST} www.alterativephotography.com$
RewriteRule (.*)$ "http:\/\/www.alterativephotography.com\/$1" [R=301,L]
My two questions are:
-When i change the AddHandleer row and add .html at the end like this:
AddHandler application/x-httpd-php5 .php .html
Then the php script works, but the server side includes stop working! How can i get both the php AND the ssi to work?
-When i corrected the typo here:
RewriteCond %{HTTP_HOST} www.alterativephotography.com
(An "n" missing, should be alterNative) then the whole site stopped working and i got a "loop error". I THINK (i'm a dummy on server stuff) that the 301 is an old redirect that is hanging on from when the site had another name. I don't need the redirect anymore, i still have the old domainname, but i have changed the DNS settings instead and have a redirect there, can i delete the 3 rows starting with "Rewrite"?
I would be very grateful for some insights on this subject!
Horizon88...
Added the code here:
http://www.alternativephotography.com/test2.php
But just get error message:
Warning: Cannot modify header information - headers already sent by...
Not sure what the problem is, php works, if you look here:
http://www.alternativephotography.com/test.php
The PHP code needs to go in a file by itself, named something like header.php. Then, you need to include header.php as an image. That's why you're getting the "cannot modify header" error, it's sending headers to the browser to tell it "Hey, an image is coming up", but then you don't show an image - you just dump all the image code right into the browser (also because you have content being output before the header() call, and it needs to be the first thing sent to the browser, but that isn't the core problem).
So, to reiterate:
Put the PHP code in a file named something like "header.php". Then, on pages where you want the image to show up, do:
<img src="header.php" />
That ought to do it.
Oh! Now it works! Great...
Thank you so much! Now... my .htaccess file looks like this:
RewriteEngine on
Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AddType application/x-httpd-php .php .html
RewriteCond %{HTTP_HOST} cyanotypes.com$ [OR]
RewriteCond %{HTTP_HOST} www.cyanotypes.com$
RewriteRule (.*)$ "http:\/\/www.alternativephotography.com\/$1" [R=301,L]
Thats ok, or can i remove the "Rewrite" stuff? I don't use the redirect any more.
Possibly. My code's untested so it might have a bug, but can you link me to where you're testing it so I can see what it's doing?
Edit: you've edited your post so it seems like it's working. Do whatever you want with the .htaccess stuff, my solution doesn't require any htaccess directives.
Sorry, forgot to edit the test2.php file, that was the problem, i edited my reply, but you had time to reply in the meantime. My fault!
The file now works with a html ending, take a look:
http://www.alternativephotography.com/test2.html
Thank you!
Woops. Edited my post while you were reposting.
No worries. Like I said, my solution doesn't require any htaccess stuff. Just make sure to mark this resolved if that's all you need (use the Thread Tools menu).
Cute cat!
It doesn't seem to work without this line in the .htaccess:
AddType application/x-httpd-php .php .html
Am i wrong?
Ok, i will, thank you for your help... final question: will any php work on the site now, then, without modifying the .htaccess? Am planning on doing a "picture of the day" random gallery feature?
AlternativePhot;10916884 wrote:Cute cat!
It doesn't seem to work without this line in the .htaccess:
AddType application/x-httpd-php .php .html
Am i wrong?
I'm not certain, but I think that line makes all your .html pages get parsed as PHP pages. I don't have a setup like that in my system and the code works fine, so you should be okay to remove it, unless there's something else going on. You might need the .php part of that line to make sure your PHP is being interpreted, but if I'm correct, you can safely remove the .html part of it.
AlternativePhot;10916885 wrote:Ok, i will, thank you for your help... final question: will any php work on the site now, then, without modifying the .htaccess? Am planning on doing a "picture of the day" random gallery feature?
Like I said, I'm pretty sure the ".html" section of that line is unnecessary. You probably need to have the line there so that Apache knows what to do with your PHP code, but other than that, my PHP code doesn't require any sort of htaccess rewriting, and general PHP code that isn't in an .html file (provided you remove the .html bit of that line) should function correctly on your site, so long as it's within a .php file. You can do something similar to what I've done with the logo code to get similar functionality with a picture of the day system.