views:

49

answers:

2

OK, I am creating a questionnaire application that is managed by a single user on /manage. There, he can create multiple questionnaires, each with a different name and set of settings(registered users, questions, data, etc.). Currently, the different tests are accessed by non-administrative users(the takers of the test) by going to /index.php?t=Questionnaire%20Name. Obviously, this is not the best interface to give to users. I would like for the users to be able to access that test by going to /QuestonnaireName. I feel like this could be done with mod-rewrite, but I don't know how to use it or where to start. I've coded the entire thing in JS, PHP, and MySQL. Any help is appreciated. Thanks.

A: 

You have to use mod_rewrite:

Example of use (it is german, I did not found any better solution): http://www.handcode.de/talks/phpug-mod%5Frewrite-20080312/slide-38.html

daemonfire300
+1  A: 

Here's a example to get you going:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

Put this in a file called .htaccess in the same directory as index.php. Now everything you call will be routed to index.php, and you can get hold of the url with $_SERVER['QUERY_STRING']. Then you are free to do anything you want with it.

Alternative way, to save you from making edits to index.php:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?t=$1

Notice the t= after the index.php?. Now everything can be found from $_GET['t'], but I can't guarantee this will be foolproof as I have not used this method myself. Test it out and see if it works for you.

EDIT

Edited to allow access to physical directories.

Tatu Ulmanen
Thanks, that worked almost exactly as I wanted it too. Really cool. However, when you try to access /manage, obviously mod_rewrite catches it and just sends you to index.php. Is there a way I can put an if statement in?
deftonix
Additionally, when I try to access /manager, the URL bar reports /manager/?t=manager. This does not happen for anything else I've tried, and /manager is the only subdirectory.
deftonix
I found out how to accomplish this. All it takes is another RewriteCond, exactly the same as the other one, but with -d, instead of -f.
deftonix
Yea, forgot to add that. Edited.
Tatu Ulmanen