How to create a page in PHP

I’m using a php script, where all pages end in "php, for example www.webpage.com/page.php

Can a page be created to show just www.webpage.com/page ?

If so, how, please? Thanks

Yes. It’s done with mod_rewrite,

First, create index.php script, which will look like this:



    $action = (isset($_GET['action'])) ? urldecode($_GET['action']) : 'home'; 

    $content = '';

    switch ($action) {
        case 'home':
            $content = 'Hello, I am home page';
            break;
        case 'contact':
            $content = 'Hello, I am contact page';
            break;
        default:
            $content = 'Page not found';
    }

    echo $content;

Add .htacces like this:


RewriteEngine on
RewriteRule ^([A-Za-z]+)/$ /index.php?action=$1 

And then try visiting these URIs:


/home
/contact
/bogus

That’s a super simple example so don’t use that in production.

This is completely untested so there might be some errors.

Correct .htaccess (I can’t edit the above post anymore):

RewriteEngine on
RewriteRule ^([^/\\.]+)/?$ index.php?action=$1 [L]

For further reference, please see our Apache Configuration which is the place for [url=“http://www.sitepoint.com/forums/showthread.php?t=183864”]mod_rewrite resources and articles such as [url=“http://www.sitepoint.com/article/apache-mod_rewrite-examples”]Learn Apache mod_rewrite: 13 Real-world Examples

Thanks for the replies. However, I’m using an existing php script. In this script you need to add to page.php to add a page. But when you add a page the browser title looks like this, for example:
http://www.webexample.com/page.php?page=12

And I’d like to add a page that looks something like this:
http://www.webexample.com/page

Can you help me? Thanks. Here’s the code for page.php:

<?php
include_once ("classes/config.php");
include_once ('classes/menu.php');
include_once ('classes/sessions.php');

$page = $_GET['page'];

//use this script to load various generic pages  (e.g. about us, terms etc)

switch ($page){
	
	case 1:
	$which_page = 'generic_aboutus.htm';
	break;
	
	case 2:
    $which_page = 'generic_contactus.htm';
    break;
    
    case 3:
    $which_page = 'generic_terms.htm';
    break;
    
    case 4:
    $which_page = 'generic_advertise.htm';
    break;
    
    default:
    header( 'Location: index.php' ) ;
    break;
    }
    
//START-----------------------
$template = "templates/main.htm";
$inner_template1 = "templates/$which_page";//middle of page
$TBS = new clsTinyButStrong;
$TBS->NoErr = true;// no more error message displayed.
$TBS->LoadTemplate("$template");
$TBS->MergeBlock('mp', $recent);
$TBS->MergeBlock('blkfeatured', $result_featured);
$TBS->Render = TBS_OUTPUT;
$TBS->Show();
@mysql_close();
die();
?>

PHP cannot interpret the address in the way that you require.
It is the web server (commonly an Apache server) that needs to be configured using mod_rewrite, so that requests to the nice address become internally redirected to the querystring address.

The greatest extent of change to your PHP code, is changing the web page links to point instead to the nicer looking address links.
Before you can do that, you need your server to rewrite those clean address links so that they redirect to their appropriate PHP pages.

I refer you back to our Apache Configuration forum, and their [url=“http://www.sitepoint.com/forums/showthread.php?t=183864”]mod_rewrite resources sticky thread.

… which is where I would refer you, too, as I have the CORRECT mod_rewrite code (not a PHP kludge) for exactly what you’re asking for (also linked in my signature).

Regards,

DK