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 "../"?

ghbarratt
  • 11,496
  • 4
  • 41
  • 41
Max Hudson
  • 9,961
  • 14
  • 57
  • 107
  • 2
    what you want to do is to use a apache modrewrite [Creating dynamic URLs in htaccess](http://stackoverflow.com/questions/5845609/creating-dynamic-urls-in-htaccess/5847426#5847426) see my answer here – Ibu Jul 10 '12 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 – Max Hudson Jul 10 '12 at 16:59
  • there is no need, you can simply upvote the answer if it works for you. – Ibu Jul 10 '12 at 17:00

1 Answers1

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]
ghbarratt
  • 11,496
  • 4
  • 41
  • 41
  • how can i use this with multiple get variables? – Max Hudson Jul 13 '12 at 18:03
  • 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. – ghbarratt Jul 13 '12 at 19:17
  • 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)? – ghbarratt Jul 13 '12 at 19:32
  • 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 – Max Hudson Jul 13 '12 at 19:40
  • 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 – Max Hudson Jul 13 '12 at 20:29
  • I think we just need some more question marks. Try my latest update (notice more ?s). – ghbarratt Jul 13 '12 at 21:14
  • how can I make the i variable a string instead of a number? when i use anything other than an integers I get a 404 error. – Max Hudson Jul 25 '12 at 01:59
  • @maxhud Change the `\d` to a `\w` in the regex. [Explanation here](http://www.regular-expressions.info/charclass.html#shorthand) – ghbarratt Jul 25 '12 at 17:23
  • perfect. thanks. 1 last thing. could you please please please give an example with 3 variables? – Max Hudson Jul 26 '12 at 04:25
  • `RewriteRule ^(\w+)?/?(\w+)?/?(\w+)?/?$ ../question.php?l=$1&i=$2&j=$3 [QSA,L]` – ghbarratt Jul 26 '12 at 13:04