Forum Moderators: phranque

Message Too Old, No Replies

Newbie help

rewriterule with rewritemap

         

dunnma

7:53 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



Ok...I am sure this is "simple" but I am just at a loss here.

The short of it is that I want to be able to have

domain.com/user

translated into

domain.com/user.php?u=id

Here is my current attempt:

RewriteRule ^([^/.]+)\/*$ user.php?u=${user_ids:${lowercase:$1}¦0} [L]

It works 100% fine except that if the username contains a . it doesn't work (gets a file cannot be displayed).

This is my last rule so basically if there is anything after the url i am going to assume that it is a user and redirect there (i would have previously processed it earlier if not).

Any thoughts?

Thanks!

jdMorgan

11:13 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rewriterule pattern explicitly excludes periods.

I would suggest using a "positive-match" approach, allowing only alphanumeric characters, plus hyphen and periods within usernames, and using [NC] to make the pattern case-insensitive for efficiency:


RewriteRule ^([a-z][a-z0-9.\-]*[a-z0-9])$ /user.php?u=${user_ids:${lowercase:$1}¦0} [NC,L]

Note that the pattern requires usernames to start with a letter, followed by any of the allowed characters, and ending with a letter or a number only.

As coded, the pattern requires at least a two-character username. To require at least three characters, change the "*" on the middle sub-pattern to a "+". If you want to require more, use the "{min,max}" regex quantifier notation, e.g. "{2,4}" for four to six (total) characters, or "{3,}" to require at least 5 total characters, but set no upper bound.

This code --as in your example-- is intended for use in .htaccess (but note that the RewriteMap must be defined in httpd.conf or conf.d).

Jim

dunnma

12:52 am on Apr 18, 2008 (gmt 0)

10+ Year Member



I actually have my stuff in the .conf file (well it is included).

My problem is that user name's were allowed to be chosen prior to the "switch" so there is no criteria for what they contain.

Basically if it has gotten to this rule then if there is anything after the domain (after the first /) then I am going to assume it is a user and will lookup accordingly.

The code that I had before this one worked as well, but it required an end /. So if there is anything after www.webmasterworld.com/ then we want it to rewriterule it. Maybe I need to do some sort of rewrite condition first to see if there is something there, then if there is, run the rule?

I am surprised I made it this far and at this point I am just nit picking having to have the trailing / after a user name (I want to make it optional).

Thanks for the help!

dunnma

5:11 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



Anyone able to help me figure out the syntax to only rewrite if there is a value after the domain (with an optional trailing /)?

so process:

domain.com/a
domain.com/hello world
domain.com/hi/

don't process:

domain.com/

jdMorgan

2:17 am on Apr 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^(.+)/?$

See the regular-expressions tutorial cited in our forum charter for more info.

Jim

dunnma

12:40 pm on Apr 20, 2008 (gmt 0)

10+ Year Member



Actually that doesn't work. I have tried it before and if someone goes to just the root domain, so www.domain.com/ it still processes.

It can only process IF there is something after the first /

I think that is why the original example I posted does work because it says to ignore the . (which index.* contains)

jdMorgan

1:21 pm on Apr 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For use at server-config level:

^/(.+)/?$

See the regular-expressions tutorial cited in our forum charter for more info.

Jim