I built a website some time ago for a company and its pages are primarily in html with a few php pages to support some dynamic content.
The company now requires dynamic content on their index.html page and although I was under the impression I could just open up some standard php tags, add in my lines of php and away you go, it appears that's not the case and my php will not run.
Can anyone explai开发者_高级运维n what is required in order for my php code to run within a .html page?
Thanks, Jonny
The server needs to be told to run the document through the PHP engine.
This is usually done by giving the file a .php
extension.
You can also configure the server to pass files other other file extensions or specific files through PHP, but how you do that depends on the server (and is a matter of server configuration, not programming).
You have two options to get where you need to go:
- Use HTACCESS to rewrite .html endings to .php
- Make .html files usable for PHP code
For process sake, and ease of support I would do number one, here are the solutions for both:
Mod-rewrite (.htaccess file), you will need equivalent .php files for all your .html files.
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php
Add this to your Apache config file:
AddHandler application/x-httpd-php .php .html .htm
I assume that the server in question already has PHP installed, as you say you have PHP elsewhere in the site.
Are you saving the HTML page as a PHP page, i.e. with the .php extension?
If the server has a PHP module installed, you could configure the web server to process .html files through that PHP module. (With AddHandler
for Apache for example)
Otherwise, rename the files to .php and have some fancy URL rewriting that allows them to be called via .html.
The easiest way is to name them .php but if you need to keep them as .html you could use Mod Rewrite on your server allow the .html links to be perfectly valid.
Update: As your on a Windows Server you could use this package http://www.seoconsultants.com/windows/isapi/ to allow rewriting of the URLs. It works much the same as Mod_Rewrite from what I can gather.
put redirect code in index.html redirect to a file with .php extension and put all the index.html material there and put whatever addtional code you want to put in it.
so when someone reaches index.html, they'll be taken to suppose home.php where the index.html content will stay and your dynamic code.
this is the simplest solution if u cant change the index.html to index.php.
精彩评论