Find the answer to your Linux question:
Results 1 to 7 of 7
Let me preface by saying that I am completely new to regular expressions and mod_rewrite. I've been looking around for a while and I haven't found anything posted that could ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    4

    mod_rewrite, need regex help


    Let me preface by saying that I am completely new to regular expressions and mod_rewrite.

    I've been looking around for a while and I haven't found anything posted that could help me specifically. Heres what I'd like:

    URL typed into browser:
    www.website.com/user/brian/?extra=1

    Rewritten URI
    www.website.com/users.php?user=brian&extra=1

    Basically looking to have the initial URL be static, but with the possibility of a dynamic variable appended to the end.

    I know the basics of mod_rewrite from tutorials I've found online, but can't seem to figure out this specific problem.

    Thanks,
    Brian

  2. #2
    Just Joined!
    Join Date
    Apr 2009
    Posts
    90
    /^(www.website.com)/user/(.+)/?(.*)$/

    $1/users.php?user=$2&$3

    ?? maybe

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    4
    I tried something similar already with no luck. Gives me a 404.

    This is what i tried:

    RewriteRule ^user/(.+)/?(.*)$/ users.php?user=$1&$2

  4. #4
    Just Joined!
    Join Date
    Apr 2009
    Posts
    90
    Close.

    RewriteRule ^(.*)user/(.+)/\?(.*)$ $1users.php?user=$2&$3

  5. #5
    Just Joined!
    Join Date
    Apr 2009
    Posts
    4
    Still no luck. Did you manage to get that working yourself?

  6. #6
    Just Joined!
    Join Date
    Apr 2009
    Posts
    4
    I'm getting close, but can't seal the deal.

    RewriteRule ^user/([^/\.]+)/refer=([^/\.]+)/?$ users.php?user=$1&refer=$2 [L]

    Is working for the example URI
    /user/testname/refer=3

    As soon as i try to add in the "?" character it fails and gives me a 404.

    DOES NOT WORK:
    RewriteRule ^user/([^/\.]+)/\?refer=([^/\.]+)/?$ users.php?user=$1&refer=$2 [L]

    any advice?

  7. #7
    Just Joined!
    Join Date
    Apr 2009
    Posts
    90
    My Regex worked fine for the example that you gave.

    In your example above, you need to ESCAPE the ? character, as ? is used in Regex to ask if there are 0 or 1 occurances.

    Try:

    RewriteRule /^.*user/(\w+)/[\?](.*)$/ users.php?user=$1&$2

    That should move the username, and keep the rest of the URL intact, including after the "?"

Posting Permissions

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