3

Say I have a user database and don't want to use get to get the id (user.php?i=234) and want to use folders and index.php(s) instead (user/234)

I can do this by creating a new folder and index.php file in that folder when the user registers. I can put an include inside the index.php file (include '/users.php') and call a function that builds the page (buildUserPage(234);)

Is this the best way to do this?

How can I get the user id based on the parent folder if 234 is the user id?

How can I check if the url of the page is index.php so I can redirect to "../"?

3
  • 2
    what you want to do is to use a apache modrewrite Creating dynamic URLs in htaccess see my answer here Commented Jul 10, 2012 at 16:57
  • alright if you post that as an answer i will accept. post the actual code though so others can see if they come across the question Commented Jul 10, 2012 at 16:59
  • there is no need, you can simply upvote the answer if it works for you. Commented Jul 10, 2012 at 17:00

1 Answer 1

2

In the .htaccess file in the user directory, place the following:

RewriteEngine On
RewriteRule ^(\d+)/?$ ../users.php?i=$1 [QSA,L]

For your question directory .htaccess file, try this:

RewriteEngine On
RewriteRule ^(\w+)?/?(\d+)?/?$ ../question.php?l=$1&i=$2 [QSA,L]
Sign up to request clarification or add additional context in comments.

10 Comments

how can i use this with multiple get variables?
Just alter the regex. Give me an example URL and how you would like to translate it, and I can help you with the rule.
Do you plan to have an actual question directory in the file system? Where will question.php reside? I assume that l can be values other than "HTML" right? Any special rules to what l can be (letters, number, underscores, dashes, etc)?
questions will be in the top directory. I was hoping I could place the .htaccess file in the /question/ directory and have question.php in the top dir and have it function. There won't be any special characters
alright thanks. and could you please let me know how to handle it when the $_get variable(s) aren't set? because right now it say file not found when i don't set the variables
|

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.