vanishing_elephant
vanishing_elephant

Reputation: 25

Apache presenting virtual subdirectories and passing parameters to PHP script

My root directory contains a selection of PHP files: index.php, about.php etc. Each of these can take an ID parameter to customize a few of the variables on the page, such as contact phone number, email address etc.

If a visitor was to access http://myserver/joebloggs (joebloggs being a parameter, not a subdirectory), I want them to be served up with the index.php page from the root directory, but for joebloggs to be passed to index.php as the ID and the page then customized. I don't want to have to create a subdirectory for every user ID, I'd rather maintain this info in a DB and have PHP generate the pages for me.

Furthermore, when the user then navigates to the about.php page, I would like this ID to be carried over and then have the about page customized too, i.e. a link to 'about' maintains the ID parameter in the URL: http://myserver/joebloggs/about/

I've got a rough idea in my mind how to do this and have been reading up on mod_rewrite but haven't had much luck in piecing various solutions together.

Any pointers or help would be much appreciated.

Upvotes: 0

Views: 424

Answers (1)

Bartosz Grzybowski
Bartosz Grzybowski

Reputation: 1179

Try something like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/(.+?)\/about
RewriteRule . /about.php?p1=%1 [L]

RewriteCond %{REQUEST_URI} \/.*?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /index.php?p1=$1 [L]

Should do the job. Tested it on my server.

Upvotes: 1

Related Questions