paperplanes
12-28-2010, 08:33 PM
Hi guys,
So I have a script profile.php which pulls all the data about a user and prints it into a pretty template. Links to the profile area as follows: profile.php?userid=XXXX where userid is their userid in a mysql table.
What would the rewrite code be, so that instead of profile.php?userid=xxxx, the link is just as follows:
http://mysite.com/xxxx
Any help is greatly appreciated, thanks!
What have you tried so far? Can you paste some of your code so members of the forum can help you debug where you are going wrong?
paperplanes
12-28-2010, 09:10 PM
Hey 120, thanks for the response. I would if I have any code, but trouble is, all the reading I've done on Apache mod_rewrite is very obfuscating and I just cant get my head around it! I don't understand all the operators and the likes.
ShaneC
12-28-2010, 09:42 PM
Here's a detailed mod_rewrite explanation I posted a while ago for another user, hopefully it helps you!
Well it does both. See what happens is this:
The Server (aka Apache) doesn't understand http://mydomain.com/page/about-us/27. To that it will understand it as folders and sub-directories.
So, in order for Apache to understand it, the mod_rewrite transforms that into the long string listed above according to your re-write rules.
So essentially what you're doing is all the links the user sees are the nice, clean ones. The only time the long index.php?p=about...etc. links appear are internally to apache - never seen by the user.
To achieve this, you use mod_rewrite. For example lets say I've got this architecture:
http://mydomain.com/index.php?p=my-page&articleNumber=38
I don't want the user to see that mess, so I use mod_rewrite.
I want users to type it in this format: http://mydomain.com/page/<page-name>/<article-number>. So lets tell apache that's what we want:
RewriteEngine On
RewriteBase /
RewriteRule ^page/(.*)/(.*)$ index.php?p=$1&articleNumber=$2
Definitions:
(.*) - This is REGEX (http://www.zytrax.com/tech/web/regex.htm) essentially meaning that it accepts any character into that spot within the link
$ - In the inital statement this signals the end of the REGEX expression
$1... $2... $n - This signals the regex expression you are relating to. So the first instance of (.*) is $1, the second instance is $2, and so on.
That's a quick run-over of it for you. If you are interested in learning it, I highlighy recommend you check out this tutorial (http://www.easymodrewrite.com/). I used that one to start teaching myself mod_rewrite and it is very helpful.
paperplanes
12-28-2010, 11:08 PM
Wow, thank you! This is exactly what I needed, thank you.