Forum Moderators: phranque

Message Too Old, No Replies

Displaying php in a static .html page

How can I display a single page with php as a static .html page

         

riospace

2:29 pm on Oct 11, 2006 (gmt 0)

10+ Year Member



I posted the following message in another area and was told that I should post this question in the Apache forum, so here it is:

I know that you can use the following code in .htaccess to execute .html pages as PHP:

AddType application/x-httpd-php htm html

But, is there a code that can be used to accomplish running a single .html page as php instead of the above code that runs every page on the site as php?

I know that I could just change the .html to .php, but I really want to know if I can run a single .html page as php without changing the extension and without using the (AddType application/x-httpd-php htm html) code that will run all of my .html pages as php.

jdMorgan

2:36 pm on Oct 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



URLs and filenames are different 'names' for the same resource, and need not have anything in common. You can solve your problem by changing the URL (i.e. your on-page links to that page) to .html, but 'mapping' that particular URL to your .php file using mod_rewrite.

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

coopster

5:21 pm on Oct 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or use a <Files> container as I mentioned in the other thread regarding Using PHP in an .html file [webmasterworld.com].
<Files myfile.html> 
AddHandler php5-script .html
</Files>

riospace

7:21 pm on Oct 11, 2006 (gmt 0)

10+ Year Member



Thanks for the help, but I think its getting over my head. I only feel safe working in .htaccess with simple code right now.

riospace

7:10 am on Oct 12, 2006 (gmt 0)

10+ Year Member



I changed my homepage to .php and I set up the 301 redirect as follows:

redirect 301 /index.html http://www.example.com/index.php

I also directed all of my old internal links to ../index.html to /

Now no matter if you type in example.com or click on an internal link / the address goes to example.com/index.php never simply site.com. Will this cause any search engine problems? If I take the redirect out then it will serve the php page without the index.php. Does this matter and why do my external and internal links only go to example.com/index.php and not simply example.com when I have 301 redirect active even if I am not typing in the old index.html extension?

[edited by: jdMorgan at 12:19 pm (utc) on Oct. 12, 2006]
[edit reason] Example.com [/edit]

spinnercee

1:11 pm on Oct 12, 2006 (gmt 0)

10+ Year Member



Add index.php to the DirectoryIndex directive:

DirectoryIndex index.php index.html welcome.html

DirectoryIndex is evaluated left to right, so if you have a file called /index.php a request for example.com/ will return the PHP page but not change the URL from example.com/ even if an index.html exists in /.

riospace

6:55 pm on Oct 12, 2006 (gmt 0)

10+ Year Member



How do I edit my DirectoryIndex directive? This is all new to me. Thanks for all of the great help. I am almost there.

jdMorgan

10:44 pm on Oct 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're on shared hosting, create a plain-text file, place the DirectoryIndex directive in it, and upload it to your server. On the server, rename it to ".htaccess".

You'll need to beware of a couple of things: First, this file may already exist, and second, it may be hidden by your host. If that's the case, you'll need to unhide it first, download it, and then add the DirectoryIndex directive to it, again, with a plain-text editor, not a word processor. Instructions on how to unhide system files (identified by the leading dot) vary from FTP client to FTP client.

Jim

riospace

11:56 am on Oct 13, 2006 (gmt 0)

10+ Year Member



Thanks Jim, I added the following code into my .htaccess file and everything seems to be working great now:

DirectoryIndex index.php index.html

Is this code correct and do I need to have more possibilities in the code, for example index.htm?

Also, does this code tell the server to check every page on my site for .php file first? If so, will this be a resource problem since only 2 (my homepage and news page) of my approx. 5,000 pages have the .php extension? The entire reason that I added the .php extension to my 2 dynamic pages was because I was told and I read that the following code, that I was using, would drain resources since it would check every page on my site for php first:

AddType application/x-httpd-php htm html

I thought it probably was not a good idea to use the AddType code since, again only 2 of my pages contain php code.

Did I do the right thing in changing my extension to .php on the 2 pages and adding the DirectoryIndex code and taking out the AddType code?

Will any of this make a difference to search engines?

jdMorgan

1:35 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The DirectoryIndex directive will only be invoked if a directory index URL is requested, e.g. example.com/ or example.com/widgets/ and not if a named-file URL is requested, such as example.com/page.html or example.com/widgets/mywidget.php.

All of these configuration directives are processed as part of the Apache API, and Apache itself is fairly "light" as far as program size, complexity, and CPU load go... You'll often see people using an old junker PC to host sites with Apache, because it's not a CPU-intensive job until you load up the site with scripts and database functions. So an old 400MHz PII machine can actually handle a fairly large site.

As far as adding ".htm" or other extensions -- only you know the answer. If you don't use that extension for directory index files, then you don't need to add it.

BTW, for best search engine results, always link to your directory index pages using the "/" URL, and not by using "/index.whatever". We've got several other SEO-related threads on domain and page URL canonicalization problems and resulting duplicate-content issues going right now, and I recommend that you read them to prevent future problems. Here's one: Duplicate Content - Get it right or perish [webmasterworld.com]

Jim

riospace

10:09 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



Should my 301 redirects have the / at the end:

redirect 301 /index.html/ http://www.example.com/index.php/

or without:

redirect 301 /index.html http://www.example.com/index.php

Also, how would I wright a 301 redirect so that my example.com will always be send to www.example.com?

jdMorgan

10:34 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A trailing "/" indicates a directory. Is index.html a directory or a page?

We have dozens if not hundreds of threads [google.com] here about www to non-www redirects and non-www to www redirects.

Jim