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.
- 04-16-2009 #1Just 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
- 04-16-2009 #2Just Joined!
- Join Date
- Apr 2009
- Posts
- 90
/^(www.website.com)/user/(.+)/?(.*)$/
$1/users.php?user=$2&$3
?? maybe
- 04-16-2009 #3Just 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
- 04-16-2009 #4Just Joined!
- Join Date
- Apr 2009
- Posts
- 90
Close.
RewriteRule ^(.*)user/(.+)/\?(.*)$ $1users.php?user=$2&$3
- 04-17-2009 #5Just Joined!
- Join Date
- Apr 2009
- Posts
- 4
Still no luck. Did you manage to get that working yourself?
- 04-17-2009 #6Just 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?
- 04-17-2009 #7Just 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 "?"


Reply With Quote
