Re-direct example.com/user to example.com/user/profile.php

Discussion in 'Programming' started by tlshaheen, May 30, 2010.

  1. #1
    Whats the easiest way to Re-direct example.com/user to example.com/user/profile.php? I need a way that my server can set up on its own - that is, when a user registers with my site, it automatically sets up a redirection in this manner, so that someone may access the users profile simply by typing example.com/user and not have to add all that extra stuff. My site is based on PHP, but this is a .htaccess issue isn't it? How can I write a PHP function to modify .htaccess and achieve this?

    Thanks
     
    tlshaheen, May 30, 2010 IP
  2. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1.) use .htaccess and mod_rewrite
    
    # for example, rewrite /user/42 to /user/profile.php?id=42
    RewriteEngine On
    RewriteRule ^/user/(\d+) /user/profile.php?id=$1 [L]
    
    Code (markup):
    2.) yes ^^
    3.) since .htaccess is a file on your file system, you could read and write it with php (as long access belongs to you) but i would not recommend this way unless you really understand what you are doing with RewriteRules. Just edit your .htaccess file in the way shown at 1.) and you are done
     
    flexdex, May 31, 2010 IP
    tlshaheen likes this.
  3. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Works great, thanks! Just so you know though, I removed the first slash in the RewriteRule, so its ^user/(\d+) after looking at other examples and such, because it wasn't working with the slash - but it works without it. Also changed other variables in it, but thanks for a good working example!
     
    tlshaheen, May 31, 2010 IP