Help with mod rewrite anyone?

Discussion in 'Web Design and Development' started by moviedrome, May 1, 2009.

  1. moviedrome macrumors newbie

    Joined:
    May 1, 2009
    #1
    Hi, sorry if this is really basic but i'm a bit confused.

    I currently use modrewrite as follows:

    RewriteRule ^([0-9]+) http://www.mydomain.co.uk/?id=$1 [nc]

    which as i'm sure you're aware turns this http://www.mydomain.co.uk/123 into http://www.mydomain.co.uk/?id=123

    Well i have an issue because i'd like to have it kind of like an if statement:

    i'd like http://www.mydomain.co.uk/cheese to rewrite to http://www.mydomain.co.uk/?name=cheese but if it's numeric as above http://www.mydomain.co.uk/123 to still go to http://www.mydomain.co.uk/?id=123

    So the variable would be ?name= if text (and numeric, i'd still want it to be ?name= if the word after the / was cheese123cheese) but ?id= if solely numeric

    is this possible?
    hope that makes sense
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Just add another rewrite line after it, you can have multiple.
    Code:
    RewriteRule ^([0-9]+)$ http://www.mydomain.co.uk/?id=$1 [nc]
    [COLOR="Blue"]RewriteRule [B]^([a-z].*)$[/B] http://www.mydomain.co.uk/?name=$1 [nc][/COLOR]
    This will make sure the first character is a letter, then anything can appear afterward.
     
  3. moviedrome thread starter macrumors newbie

    Joined:
    May 1, 2009
    #3
    Thanks very much, that solves the issue if it's /cheese123cheese (lol sorry for my crap examples)

    how would i get it to do it if it was /123cheese123

    basically i only want it to go to ?id= if there is only numbers after the / otherwise i need it to go to name (or is this not possible?)
     
  4. dbell macrumors member

    Joined:
    Jul 11, 2007
    #4
    Check out RewriteCondition, you can add conditional logic to your RewriteRules with it.
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Dang, somehow I knew you were going to ask that ;) Notice I added the $ to the end of the regex part of the first rewrite, which means all characters must be numeric. So that part should be fine. So lets fix up the second one to handle this new requirement.
    Code:
    RewriteRule ^(.*(?=[a-z]).*)$ http://www.mydomain.co.uk/?name=$1 [nc]
    The regex here will make sure that there is at least one alpha character. I think that should handle your cases, though I'm not 100% sure the rewrite can handle this more advanced regex syntax.
     

Share This Page