Results 1 to 15 of 15
  1. #1

    Question Mod Rewrite Help Required

    Hey guys,

    I'd like to write a Mod Re-write to redirect a user from: www.domain.com/username to: www.domain.com/path/?id=$username

    But if I use:

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

    It messes up all my other paths. Is there any way to do this?

    Thanks for your time,
    Jon

  2. #2
    Hi,

    Do you want to only affect certain patterns? e.g. only www.domain.com/username? in this case use the following rule:

    RewriteRule ^(username) path/index.php?id=$1

    If not, how can you tell the difference between something you want to redirect and something you don't?

  3. #3
    That's my problem, I don't know how to tell the difference. Maybe I need to use another method.
    Coding my way to oblivion.

  4. #4
    Join Date
    May 2005
    Location
    Canada
    Posts
    47
    put something before the username like...

    www.domain.com/members-username

    then you can use something like this..

    RewriteRule ^members-([a-z]*).* path/index.php?id=$1 [L,NC]

  5. #5
    Ya, I wanted to eliminate the need to use /user or /member, but I don't think it's going to happen, at least not with mod-rewrite.
    Coding my way to oblivion.

  6. #6
    Hi,

    How about enforcing a rule on other pages of your site, such as they must contain a slash or a dot, usernames must not contain a slash or a dot. Your rewrite rule could then be:

    RewriteRule ^([^\./]+) path/index.php?id=$1

    This will correctly interpret the following as normal pages from your site:

    http://www.domain.com
    http://www.domain.com/
    http://www.domain.com/page.html
    http://www.domain.com/somefolder/page.html
    http://www.domain.com/somefolder/a_file

  7. #7
    Hey guys,

    I'd like to write a Mod Re-write to redirect a user from: www.domain.com/username to: www.domain.com/path/?id=$username

    But if I use:

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

    It messes up all my other paths. Is there any way to do this?

    Thanks for your time,
    Jon
    To redirect www.domain.com/username to www.domain.com/path/?id=$username you would need to use the following rule:

    RewriteRule ^/(username)$ /path/index.php?id=$1 [L]

    The rule I gave out will work ONLY if you use mod_rewrite with httpd.conf.
    Mod_rewrite works differently with .htaccess (more information is available at www.apache.org).
    To make the rule compatible with .htaccess, you'd need the following:

    RewriteEngine on
    RewriteBase /
    RewriteRule ^(username)$ path/index.php?id=$1 [L]

    Also, the rule will now apply ONLY for www.domain.com/username and NOT to www.domain.com/username/
    To catch both username and username/, you'd need something like this:

    RewriteRule ^(username) path/index.php?id=$1 [L]



    When using mod_rewrite, be careful with flags since you may have the right regexp and correct rule, but flags (like [L] which stands for Last) might be incorrect or missing.

  8. #8
    I can't seem to get any of these to work. Is username the proper word, is that just an example?
    Coding my way to oblivion.

  9. #9
    RewriteEngine on
    RewriteBase /
    RewriteRule ^(username)$ /path/index.php?id=$1 [L]


    Try that one.
    Also, don't just use given examples as working stuff and try to learn what each line means (meaning ask what you don't understand).
    That way you'll be able to make your scripts working on your own.

  10. #10
    Oh, I get it now. Username is the actual username, duh!

    But the thing is, I may have a 1000 usernames, will I have to write a script to add the unique username to each line, or is there some way I can do this dynamically? That's what I was trying to do.

    So then I could do..

    www.domain.com/user1
    www.domain.com/user2

    But maybe I'll just have to add those to my .htaccess file when they sign up.
    Coding my way to oblivion.

  11. #11
    Why don't you user's ID from database instead of his/hers nickname?
    Additionally, you could do this:

    RewriteRule ^username/(.*) /path/index.php?id=$1 [L]

    That would capture any string combination in url that looks like this: http://www.domain.com/username/actua...name_goes_here

    (You would probably apply some more filtering to that string combination).

    But if I were you, I'd use user's ID (integer) for manipulation with users instead of username.

  12. #12
    Well, the reason i want to user a username is so that the user can have a nice, easy to remember URL, like www.domain.com/username.

    And I know how to do it with a starting path, like /user/username, I was just trying to get away from having to use that extra path.

    Why do you think an integer would be better than their username?
    Coding my way to oblivion.

  13. #13
    Well, mainly because of the stress to the database.
    Integers are easier to index, therefore overall performance would be faster (if you index a varchar/text field then a hash has to be calculated, taken in the account you've indexed it).

    But, why don't you do something like username.domain.com?
    It's accomplishable with mod_rewrite.

  14. #14
    oh, that's even nicer. Care to show me how to do that?

    ps. you've been a great help so far, I appreciate your time.
    Coding my way to oblivion.

  15. #15
    Unfortunately, I'm going off at the moment so only thing I can do for you today is to give you the following link: http://www.webmasterworld.com/forum92/3555.htm

    There's a simillar case for creating a subdomain, you might want to try and modify it.
    If I catch some time, I'll try to put up some text and post it here.
    Good luck with your script and website

Posting Permissions

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