homepage Welcome to WebmasterWorld Guest from 54.227.34.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Converting an HTML Static Website into a PHP website
Can this be done? Time & Costs Involved?
contentmaster

10+ Year Member



 
Msg#: 4618100 posted 11:48 am on Oct 21, 2013 (gmt 0)

Hello! Before I post my question here, I'd like to mention that I have little to no knowledge about programming languages.

I run an HTML website with 600 to 1000+ pages. Over the years, it has become increasingly difficult to manage such a large resource manually. Updating pages and tweaking the page design, etc takes too much time. Lately, I have been looking at a lot of simple, well designed websites made in PHP.

I want to know if it is possible to slowly convert my current HTML website into PHP?

If yes, what would be the costs involved?
How much time can this take?
Are there any companies that offer this service?
How safe or unsafe will this exercise be keeping in mind that I do not want the site to have any down time & of course, I would not want any loss of data.

Any help will be much appreciated.

 

Swanny007

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4618100 posted 3:31 pm on Oct 21, 2013 (gmt 0)

I converted one of my sites from static HTML to PHP just for template purposes (easier to maintain). It was only about 100 pages though.

What I did is used the .htaccess file to keep the URLs ending with .html but serve them as PHP pages server-side. It's important to keep the same URI's if at all possible. If that's not possible, be sure to 301 redirect the pages to the PBP pages.

- The cost depends on whether you can do it yourself.
If the static pages use the same template, then you could do a find and replace on the common page areas. Use something like <?php include ('header.php'); ?> in place of the top header stuff, do the same for footer, head section, etc.
- Time? Hard to say. If you can find and replace it won't be too bad. If not, you will have to manually edit each 6000-1000+ pages.
- I'm not sure if there are companies that would do this but I imagine there are some web design places that could help.
- It's safe as long as the page URLs don't change (or if they have to, do a proper 301 redirect and update *all* links)

Some folks will probably reply here to tell you to use a CMS. Personally I would say don't switch to a CMS like WordPress unless you need to. Keep it as PHP pages will ensure a more secure, simpler site. No database, no script updates, easy to maintain.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4618100 posted 4:35 pm on Oct 21, 2013 (gmt 0)

What I did is used the .htaccess file to keep the URLs ending with .html but serve them as PHP pages server-side.

Did you rewrite to php, or throw in an AddHandler line to parse all html as php? They're subtly different approaches but the visitor won't know either way.

The huge advantage to keeping the same URL is that you don't need to make changes all at once. You can sneak up on it slowly by, for example, first replacing recurring material with SSIs ... and then going one step further to replace those with php includes ... and then selectively replacing some html pages with php constructs.

Search engines see a lot of things that your ordinary human visitor doesn't notice. But not even a search engine knows when it has been rewritten.

contentmaster

10+ Year Member



 
Msg#: 4618100 posted 5:06 pm on Oct 21, 2013 (gmt 0)

Thanks for your replies! Considering how large the website is, I do think the costs would be rather high if I try to outsource this. Hence, I am tempted to try doing this myself. Having said that, I'd like to mention that I have a zero programming background.

1. Do you think I can read about this and then try doing it myself?
2. If yes, what's the best way to go about this?
3. Any resources you can point me to so that I can try reading and learning?

If not, should I consider this a lost cause and try to find a company that can help?

Thanks.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4618100 posted 8:41 pm on Oct 21, 2013 (gmt 0)

Do you think I can read about this and then try doing it myself?

Did you make the html yourself? If so, you should be able to handle the php. The crucial difference is that html is functionally a word processor while php is a language, which means zero tolerance. One missing semicolon, one parenthesis by mistake for a brace, and you'll get a blank screen staring you in the face.

If you don't already have a pseudo-server like MAMP or WAMP, get one. The free version will do. You can view html in any text editor's html preview, but for php you'll need a server.

Swanny007

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4618100 posted 2:21 am on Oct 22, 2013 (gmt 0)

lucy24, I use this one line:
AddType application/x-httpd-php5 .php .html

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4618100 posted 3:08 am on Oct 22, 2013 (gmt 0)

Ah, you're parsing html as php. That's probably the easiest way to go if most of your pages end up containing php (in the page itself, not an include). But it will slow down the server a bit if only a tenth of your html pages actually use php. Come to think of it, it's much the same trade-off as allowing SSIs in .html instead of changing everything to .shtml

Generic rule: If you're asking the server to look for something, it should either be something the server is fairly likely to find, or something so important that it's worth the extra work.

jinxed

5+ Year Member



 
Msg#: 4618100 posted 6:37 am on Oct 22, 2013 (gmt 0)

Learn PHP (or similar) in small steps. Trust me it will make your life easier in the long run.

I have personally stayed away from popular cms's because I find there is too much code redundancy for what I need and I like to know what *all* the code is doing!

contentmaster

10+ Year Member



 
Msg#: 4618100 posted 10:41 am on Oct 22, 2013 (gmt 0)

Did you make the html yourself? If so, you should be able to handle the php


Yes, I can manage HTML but this seems like a daunting task especially because I am not comfortable with excessive coding.

Learn PHP (or similar) in small steps.

I would like to try. Can you point me in the right direction? Where & How to begin? Any resources that would be useful?

Swanny007

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4618100 posted 3:31 pm on Oct 22, 2013 (gmt 0)

Yes, I can manage HTML but this seems like a daunting task especially because I am not comfortable with excessive coding.

Once you get the hang of converting the first few pages the rest are easy. If you're just using PHP to use a template system, it's really easy and you don't need to learn a bunch of PHP, just the basics on how things work to do the include files.

Here's an example of what your converted HTML/PHP page could look like:

<html>
<head>
<title>Page title</title>
<?php include('/inc/head.php'); ?>
</head>
<body>
<?php include('/inc/header.php'); ?>
<h1>Page title</h1>
<p>Page content</p>
<?php include('/inc/footer.php'); ?>
</body>
</html>

Then you just use the head.php file to include the common head elements like the stylesheet, the header.php to setup the top part of the template (e.g. navigation bar), and the footer.php to wrap up the bottom template stuff. Then if you want to make changes to the template, you just update one or more include (inc) files and it's live on the whole site. In my example, the inc is just the folder where you store the template files.

I use that sort of thing to use a single template on the whole site. If I understand that's basically what you're trying to do.

php.net is the main PHP website, but there are some other good tutorial sites out there, just search something like "beginner php tutorials" in your favorite search engine to get started.

jinxed

5+ Year Member



 
Msg#: 4618100 posted 4:18 pm on Oct 22, 2013 (gmt 0)

+1 for starting with some simple includes. Avoiding code duplication is a massive benefit.

I remember the days of trying to find the best 'search and replace' tool. Not very productive!

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4618100 posted 5:49 pm on Oct 22, 2013 (gmt 0)

One nice thing about php is that it has a very shallow learning curve. That means: If you know how to do one thing, you can have pages doing that one thing, with no need to think about the 999,999 other things that php can do. Then, when you learn how to do a second thing, you can incorporate that second thing, still without having to change anything else.

Longtime readers know that I mean the preceding quite literally. In my case, #1 thing was: make a "Contact" page that processes email so users don't have to resort to mailto: helpers. And then #2 thing was: check a variable (in this case the page title) against a simple if/then loop to make page-specific navigation footers. #3 thing was: output buffer, so the contact page will display a different header depending on whether the user's message was sendable or not, and built-from-scratch pages will display the 404 page if the request is bad. (#4 thing will be: have the whole contact process take place on one page instead of two. Haven't got there yet ;))

Readie

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4618100 posted 9:56 pm on Oct 22, 2013 (gmt 0)

You can view html in any text editor's html preview, but for php you'll need a server.

Not entirely true. There's a few pretty nifty applications out there useful for developing PHP. A favourite one of mine is called "EasyPHP" - it's dirt simple and allows you to execute PHP scripts on your local machine.

----

As a solution, I'm going to give you a simple example of Swanny's suggestion, as for someone of your experience it's an excellent starting point (and is pretty similar to what the dude who got me started in this had me do as a first script come to think of it)

Before I begin this, I'll just give an honorable mention to how every coder learns how to do something new in a language. We Google "PHP <<PROBLEM>>".
So, say I didn't know how to list files in a directory in PHP, I would Google "PHP list files in directory" - and chances are one of the top 5 results is a Webmaster World or Stack Overflow page with someone who once wanted to do exactly the same thing you did - with a helpful solution or link to a solution below.

---

Anyway... we'll assume you have no solution currently implemented. The first step is to create a plain text file called ".htaccess" in the root directory of our site.
.htaccess
AddType application/x-httpd-php5 .php .html

Now PHP code can be written into .html files and will execute as PHP rather than print as text.

Now we'll move on to actually converting the pages. First up is a totally legitmate copy of your about us page, which for the sake of argument, you have saved in wwwroot/pages/:
about_us.html
<!DOCTYPE html>
<html>
<head>
<title>Boyd&#39;s Toast :: About Us</title>
</head>
<body>
<h1>Boyd&#39;s Toast</h1>
<p class="small">The best toast on the East coast</p>
<hr>
<h2>About Us</h2>
<p>We make the best toast ever.</p>
<p><strong>Ever.</strong></p>
<hr>
Copyright &#169; Boyd&#39;s Toast
</body>
</html>

There's 2 segments of that file which can pretty easily be pulled out into seperate files.

So. Let's create these 2 new files:
header.php
<!DOCTYPE html>
<html>
<head>
<title>Boyd&#39;s Toast :: <?php echo $pageTitle; ?></title>
</head>
<body>
<h1>Boyd&#39;s Toast</h1>
<p class="small">The best toast on the East coast</p>
<hr>
<h2><?php echo $pageTitle; ?></h2>

footer.php
<hr>
Copyright &#169; Boyd&#39;s Toast
</body>
</html>

And we'll save these 2 files in wwwroot/include/

We've now created 2 files that contain the common elements of all our pages. Now we need to modify about_us.html to make use of them. There are 2 ways of doing this, using either php's require method, or php's include method. They're very similar in use (just change require for include or vice versa) but they behave a bit differently.

Include - if it fails to locate the file, will log a warning, and allow the script to continue.
Require - if it fails to locate the file, will log a fatal error and cease further script execution.

I personally favour require, as I know I have the file then.

You'll also notice that in header.php I am echo (an equivilent to "print" in PHP) 'ing $pageTitle. This is a variable in PHP, so before including header.php, I will need to initialise this variable.

So, with all that in mind, we can rebuild about us, to this:
NEW about_us.html
<?php

$pageTitle = 'About Us';
require '../include/header.php';

?>
<p>We make the best toast ever.</p>
<p><strong>Ever.</strong></p>
<?php

require '../include/footer.php';

?>




A further note on require / include:
Not preceeding the file path with a forward slash, as in the above example, will cause PHP to look for the included file in relation to the current file (what is called a relative path)
Preceeding the file path with a forward slash will cause PHP to look for the included file relative to the server root (note site root - important distinction) - this is called an absolute path.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4618100 posted 11:30 pm on Oct 22, 2013 (gmt 0)

One thing you will need to also do is make sure that every link to a CSS or JS file or to an image begins with a leading slash and mentions the full path to the file.

contentmaster

10+ Year Member



 
Msg#: 4618100 posted 1:18 pm on Oct 27, 2013 (gmt 0)

Hello everyone! This is precisely the reason why I am such a big fan of Webmasterworld. Thanks for all the help, guidance and encouragement, appreciate it.

I will start learning and experimenting with all the information above. Will keep you posted on my progress. Thanks again!

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4618100 posted 3:42 pm on Oct 27, 2013 (gmt 0)

You can view html in any text editor's html preview, but for php you'll need a server.


Not entirely true. There's a few pretty nifty applications out there useful for developing PHP. A favourite one of mine is called "EasyPHP" - it's dirt simple and allows you to execute PHP scripts on your local machine.


@Readie: Just curious about this "EasyPHP" you mentioned. The only "EasyPHP" I can find appears to be a wrapper for an Apache server environment (WAMP stack)?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved