views:

31

answers:

1

I want to have a main section which is found on services.php and then some sub-sections like services_logodesign.php.

I want to make url's look like: When I click a button to open services_logodesign.php the browser should show "services/logo design"

I have several other sub-sections, it will be nice to have one code for any other pages.

A: 

The simplest way to do what you want is to have all requests redirected to what is essentially a dispatcher script -- index.php would be a good candidate for such dispatcher script.

Your .htaccess file in the root of your website, will look like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This mod_rewrite configuration will send all requests to index.php. Meaning that requests for /services/logo design/ (by the way, spaces in url paths are not a good idea) or /foo/bar or /blah/blah/blah will all go to /index.php

Your index.php script can split the url elements into an array for processing however you want.

$req = array_map( "urldecode", array_filter( explode( '/', $_SERVER['REQUEST_URI'] ) ) );

If you sent the request /bloop/bleep/blorp the line of php code above would put the following into the $req array:

array('bloop','bleep','blorp');

Or a request like /services/logo_design/ would look like: array('services', logo_design)

A simple way to process this array would be:

if ($req[0] == 'services') {
  if ($req[1] == 'logo_design') { include_once(services_logodesign.php); }
  else { include_once(services.php); }
}

See how that works? Though this is a simplified example of what you should do, it's much, much better and more secure than the way you seem to be trying to accomplish the same task.

Fred Garvin