SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: mod_rewrite

  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite

    I'm building a profile page, to access a profile of a certain you go to:
    example.com/profile.php?id=[user_id]
    e.g if a user has an id of 6
    example.com/profile.php?id=6
    each id of a member has a username associated with it in the database. I want to create this for each user:
    example.com/john/
    which will actually be this
    example.com/profile.php?id=6
    how do i do a rewriterule to accomplish this?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You can't create a RewriteRule for this, since Apache doesn't know that john has id 6.

    You can either create a RewriteRule that will just pass everything to profile.php and let php figure out what it's supposed to do using the path info (/john/), or use a RewriteMap.
    Note that you can only use a RewriteMap if you have access to the main server config (httpd.conf), so this won't work if you're on a shared host (unless the host is very flexible).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mod_rewrite is a module of Apache and it does not directly deal with MySQL (or any) database. So it is not possible to have username instead of id from within the rewrite rule itself. So you have to pas the username to the PHP file/script page then find the id of the username (possibly on top of the script page). And the rewrite rule will look like this in your .htaccess file:
    Code:
    RewriteEngine On
    ^/(.*)/?$ /profile.php?username=$1
    And in your PHP, you can get it:
    PHP Code:
    $username $_GET['username'];
    // now you can get user's id by querying in the database. 
    More details for the examples of rewrite rules, see:
    http://www.sitepoint.com/guide-url-rewriting-2/
    http://www.sitepoint.com/apache-mod_rewrite-examples/
    Last edited by Raju Gautam; Dec 30, 2011 at 04:45. Reason: Spelling and language.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand.
    Assuming I modified the code and made the request like this:
    example.php/profile?id=john
    What would the rewrite rule be?

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Then it would be

    Code:
    RewriteRule ^(.*)/$ /profile.php?id=$1
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Then it would be

    Code:
    RewriteRule ^(.*)/$ /profile.php?id=$1
    Hi, im getting a 404

    My files are all in a directory called /!

    But i want it to be example.com/john
    not
    example.com/!/john

    what am I doing wrong?
    RewriteBase /!/

    RewriteRule ^(.*)/$ /myprofile.php?id=$1

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Where is myprofile.php ? Is it in / or in /!

    I'd advice removing the RewriteBase and use the /!/ on every rule that needs it, and don't put it on others, it's just cleaner that way. Plus, RewriteBase usually causes more problems than it solves.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i will remove rewritebase.

    myprofile.php is located in /!/

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Well, then obviously the rule changes to

    Code:
    RewriteRule ^(.*)/$ /!/myprofile.php?id=$1
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Raju Gautam View Post
    mod_rewrite is a module of Apache and it does not directly deal with MySQL (or any) database.
    @Raju - incorrect. Rémon's already mentioned that the RewriteMap (program type) can access a database, flat file, anything on the server but you MUST have access to the server or virtual host configuration file as any error can bring down Apache.

    Quote Originally Posted by ScallioXTX View Post
    Then it would be

    Code:
    RewriteRule ^(.*)/$ /profile.php?id=$1
    @Rémon - you KNOW better than to use (.*) (without the trailing /) without providing a warning that it's the most often abused (and greatest cause of problems) by/for newbies! Your version will work (the op's /? won't ... without looping) but it's dangerously close to (.*) which newbies know and love despite not understanding the responsibility that comes with the power of the EVERYTHING atom!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  11. #11
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what should I be using? the given line of code does not seem to be working.

  12. #12
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    UPDATE:

    This works:
    http://www.example.com/john/

    This DOESNT work:
    http://www.example.com/john

    How can I make it work?

  13. #13
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    In that case you need to remove the trailing slash from the RewriteRule, but then your code will be loopy, so you need to add a RewriteCond to prevent that (i.e., if the request is for profile.php, don't redirect to profile.php). Something like

    Code:
    # Remove trailing slash from non-existing directory request (which are probably profiles)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/?$ /$1 [L,R=301]
    
    # Serve profiles through /profile.php
    RewriteCond %{REQUEST_URI} !^/profile.php
    RewriteRule ^(.*)?$ /profile.php?id=$1
    What characters do you accept for profile names btw? It's better to make a regular expression tuned to your needs instead of this extremely generic one.

    Note that your profile.php has to check if the given profile actually exists, and if not, reply with a HTTP 404 File Not Found.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  14. #14
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, using this im getting a 403.shtml

  15. #15
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Did you see I forgot the /!/ in the path?

    (not deliberate by the way, I just noticed it).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

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