Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
    
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. –  user1797 Sep 18 '09 at 13:23
    
I thought of something like: "RewriteRule ^user/[a-z]$ /?q=user/$1", but it doesn't work. –  user1797 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 domain.com/user/username/filename it would still redirect to domain.com/?q=user/username –  Dave Drager Sep 18 '09 at 13:47
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
This does it. Thanks! –  user1797 Sep 18 '09 at 14:45
add comment

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).

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.