1

I have a page that displays information for a selected member. Right now, I choose select the member in the URL using GET data like so: Page/index.php?Member=member1 I see websites that do it like this: Page/member1 Is this possible to do with only one PHP file?

1
  • It can be done in PHP - see my post but it is not clean and could be error prone. Commented Feb 11, 2010 at 4:52

4 Answers 4

3

Friendly URLs need some kind of processing by the webserver so that the page requests are channeled through a single php script (aka controller).

For example, Apache has the ability to match patterns in the URL and modify the request behind the scenes - you would actually write the PHP to react as if the user had entered the original GET string, but they can use the friendly URL instead.

Simple example for Apache - put a .htaccess in your document root and write:

RewriteEngine On
RewriteRule   ^Page/([A-Za-z0-9\_]+)/?$   index.php?member=$1 [QSA,L]

I don't know what the equivalent would be in IIS - but I am aware that it is available

Sign up to request clarification or add additional context in comments.

Comments

1

This can't be done with just PHP - you do this with mod_rewrite and usually .htacess on apache.

1 Comment

Post below mine answered on how to do a rewrite with apache
1

There are many ways to do this, all of which depend on the web server. With Apache, some possibilities:

  • mod_rewrite (most flexible way, but can be tedious to configure)
  • Option +MultiView (very easy, but requires PHP files to be named similar to the requested URL)
  • custom 404 handler (not recommended)

Say you are using Apache and have enabled MultiView. The file Page.php would be a match for Page/*, which would obviously include Page/member1. You would then have to inspect the URL (look at $_SERVER, it has everything you need) and decide if you can honor the request.

Comments

0

You can do it in PHP alone using the following:

1.) Get filename relative to the document root.

$_SERVER['PHP_SELF']

http://example.com/test.php/foo.bar would be /test.php/foo.bar.

2.) Explode by your delimiter (in this case /)

array explode  ( string $delimiter  , string $string  [, int $limit  ] )

3.) Verify values in array and then place into variables

var $myValue = myarray[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.