music
OSdata.com: programming text book 

OSdata.com

virtual pages: turning dynamic pages into static links

summary

OSdata RSS
News Feed


OSdata blog RSS.

OSdata blog RSS - add to Bloglines
OSdata blog RSS - add to Google Reader.
OSdata blog RSS - add to My AOL
OSdata blog RSS - add to Newsgator.

    This section discusses how to create virtual web pages by turning static links (for web addresses that don’t actually exist) into dynamic URLs for a script that does exist.

    Any browser or robot (including Google’s bot) thinks that it has reached a real static web page. Behind the scenes, Apache (or your favorite web server) converts the incoming URL into the real URL and delivers the results of the chosen script.

    This technique is used on Wikipedia. The millions of article page URLs are actually converted into parameters for a single, all-powerful script. That single, all-powerful script delivers each article to the visiting browser.

license

    This is example code from OSdata, This Side of Sanity, and Twiddledom, released under Apache License 2.0.

    Copyright © 2012, 2013, 2014 Milo

    Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

previous page   next page
previous page
Source Code Example
  next page
404 page not found redirect
Google

virtual pages: turning dynamic pages into static links

summary

    This section discusses how to create virtual web pages by turning static links (for web addresses that don’t actually exist) into dynamic URLs for a script that does exist.

    Any browser or robot (including Google’s bot) thinks that it has reached a real static web page. Behind the scenes, Apache (or your favorite web server) converts the incoming URL into the real URL and delivers the results of the chosen script.

    This technique is used on Wikipedia. The millions of article page URLs are actually converted into parameters for a single, all-powerful script. That single, all-powerful script delivers each article to the visiting browser.

.htaccess file

    The first part of our scheme is a handful of entries into your .htaccess file. Place the .htaccess file into the top level directory of your web tree (that is, the same folder that contains your normal index.html).

    This is a very short .htaccess file. We turn on URL rewriting, set the rewrite base, allow all real existing files and directories to be accessed normally, and then send everything else (all the virtual static addresses) to a central index.php to process and create the web page. Note that you can use any scripting language of your choice, not just PHP.

    Our .htaccess rules append the working part of the URL (everything after http://www.example.com) to the end of new file name (after the name of the script).

    This approach allows our script to evaluate the original static URL and act accordingly.

    Many programmers prefer to convert the original static URL into GET parameters. I have chosen this method because it is more general purpose. If we change our naming scheme, we can make the adjustments in the script rather than in the .htaccess file. Also, we can change the naming scheme for just a portion of the website and leave older working code alone.

    The one possibly strange item is the rewrite condition using a negation. This is done because it is faster than dot asterisk (.*).

    It is also possible to make the changes in the httpd.conf file rather than in the .htaccess file. Because we have placed the .htaccess file in the top level directory of the html tree, we are effectively changing it for the entire website. Making the changes in the httpd.conf file would seem more straight-forward and possibly run faster, but not everyone has access to their httpd.conf file (particularly those on shared servers). This solution is near universal. Feel free to make changes to your httpd.conf file instead.

# Turn on URL rewriting
RewriteEngine On

RewriteBase /

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteCond $1 !^(index\.php|robots\.txt)

RewriteRule ^(.*)$ index.php?/$1 [L]

homepage.php file

    The homepage.php file is the default home page. This file exists because the art director at Twiddledom.com wants to manually maintain this file separate from the rest of the web site.

    Therefore, we redirect to that page when someone attempts to arrive at www.twiddledom.com (or any of its valid alternatives) or www.twiddledom.com/index.php (or any of its valid alternatives).

    We have to do the redirect because the index.php file is the script that runs the entire website. Name clash.

    You probably don’t have this problem, but if you have any similar restrictions, you can see how to code for them. Note that this only applies to name clashes. If you have a real file (.html, .php, .rb, .pl, .py, etc.), then the .htaccess rules will send the visitor directly to the real file.

    The following file can be used until you debug this code and adapt it your own uses. You can also simply take your existing home page and rename it to homepage.php.

<?php

echo 'welcome to the home page.';

index.php file

    The index.php file is intended to control the entire website. You may use any scripting language you prefer.

    Note (again) that the .htaccess file honors all existing files and directories. You can freely mix virtual and real URLs on your website (which is great for adding this capability to an existing website without breaking things all over the place).

    In this example, error reporting is turned on. This will help you debug your work as you get all of this running for the first time. Once you have this part successfully working, comment this out because the error reporting can help crackers. Use your error log on any production server.

    The first thing we do is collect the original URL that was sent from the web browser. This is everything after the domain name.

    As just mentioned, we redirect the home page. This is done with the if block. You probably won’t need to do this, but it helps you see what is inside the $_SERVER['REQUEST_URI'] variable. Notice that it always starts with the slash (/) character.

    We then echo the fact thatwe are at a test page (this confirms that your set-up is working properly).

    And finally we echo the $_SERVER['REQUEST_URI']. This makes it easy for you to experiment with various different test URLs and see what you get.

<?php

error_reporting(E_ALL);

/* FILE NAME /index.php */

$incomingurl = $_SERVER['REQUEST_URI'];

if ( ( $incomingurl == '/index.php' ) OR ( $incomingurl == '/' )
  {
    require_once('homepage.php');
    exit;
  }

echo 'successfully at test page<br>';

echo $incomingurl;

installation

    Copy and paste the three blocks of code. The first block is stored in a file named .htaccess. The second block is stored in a file named homepage.php. The third block is stored in a file named index.php. All three files go in the top level of your web directory. Note that you may need to use a different local name for your .htaccess file (because it might otherwise disappear on you or might be rejected, such as in WIndows Explorer) and then rename it to .htaccess after uploading it to your web site or transferring it to your localhost web directory.

    The .htaccess file is also available in the source code section at dothtaccess.html. The index.php file is also available in the source code section at index.php, but it contains the whole index.php file rather than the simple example listed above. Emphasis: These pages will include more code than shown in the example.

return to source code lessons

return to Outrageous Coder
return to Source Code Examples

previous page   next page
previous page
Source Code Example
  next page
404 page not found redirect

contact
comments, suggestions, corrections, criticisms

because of ridiculous spam attacks,
contact through Twitter (@OutrageousCoder) will be more reliable than the contact form

please contact us

your name:
email address:
phone number:
message:

return to Outrageous Coder
return to source code lessons
return to table of contents
free downloadable college text book

license

    This is example code from OSdata, This Side of Sanity, and Twiddledom, released under Apache License 2.0.

    Copyright © 2012, 2013, 2014 Milo

    Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

previous page   next page
previous page
Source Code Example
  next page
404 page not found redirect

Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2014 Milo

    Created: May 22, 2014

    Last Updated: May 23, 2014


return to table of contents
free downloadable college text book

previous page   next page
previous page
Source Code Example
  next page
404 page not found redirect