Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Mod_rewrite Help

    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!

  • #2
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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?
    There is always plenty of idol work for the devils hands to do
    Registered Linux User 475075 : Project Honeypot

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Here's a detailed mod_rewrite explanation I posted a while ago for another user, hopefully it helps you!

    Quote Originally Posted by ShaneC View Post
    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-p...ticleNumber=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:

    Code:
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^page/(.*)/(.*)$ index.php?p=$1&articleNumber=$2
    Definitions:
    (.*) - This is REGEX 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. I used that one to start teaching myself mod_rewrite and it is very helpful.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • The Following 4 Users Say Thank You to ShaneC For This Useful Post:

    120 (12-29-2010), camaross (01-22-2011), Mido™ (01-14-2011), paperplanes (12-29-2010)

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Wow, thank you! This is exactly what I needed, thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •