0

I need to accomplish the following with mod_rewrite:

URI request: http://domain.com/user/username/ or http://domain.com/user/username
URI translate: http://domain.com/?q=user/username

This needs to be very specific, with no wildcards. Could an expert on mod_rewrite and regex lend a hand? Thanks!

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • How do you want to do this with no wildcards if "username" will be changing? Or is this for a specific username? – Dave Drager Sep 18 '09 at 13:15
  • What I meant was, the ruleset needs to be as specific (strict) as possible as to not interfere with another. It needs to match /user/variableusername precisely. The "variableusername" will always be lowercase alphabetic. –  Sep 18 '09 at 13:23
  • I thought of something like: "RewriteRule ^user/[a-z]$ /?q=user/$1", but it doesn't work. –  Sep 18 '09 at 13:25
  • Add answer seems to be broken, here is what I came up with: The best I could come up with is to create a /user/ directory and add this into it's .htaccess file: RewriteEngine on RewriteRule ^(\w+) /?q=user/$1 [R,L] I think there is a way to do this by putting .htaccess in the root web directory but I couldn't seem to make it work without getting into a loop. The above script will trim out anything after a "username", for example if someone requested *http://domain.com/user/username/filename* it would still redirect to *http://domain.com/?q=user/username* – Dave Drager Sep 18 '09 at 13:47

2 Answers2

1

I think you're forgetting the + after the [a-z]:

RewriteRule ^user/([a-z]+)$ /?q=user/$1 [L]

I tried on my server and it works.

hdanniel
  • 4,293
  • 23
  • 25
0

I tried:

RewriteRule ^user/[a-z]$        /?q=user/$1

But it doesn't work. Dave Drager, I am looking for a cleaner version (not sure if your approach will work, but would rather not create a directory, want all from .htaccess).

  • I'm going crazy, It won't let me add an answer, says "page doesn't exist"! Try RewriteRule ^/user/(\w+) /?q=user/$1 [R,L] – Dave Drager Sep 18 '09 at 14:23