#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29

    Mod_Rewrite for 6 variables


    I've got six variables in a url that I need to put into a clean url.

    Here's the current .htaccess file:

    Code:
    RewriteEngine on
    Options -MultiViews
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)(/)?$ /index.php?RF=$1&loc=$2&c=$3&ex=$4&jc=$5&f0=$6 [R,QSA]
    When I type in what I want to be converted:
    darkentity-static.info/DARK/ge/si/14/css/style

    It ends up at:
    http://darkentity-static.info/index.php?RF=ge&loc=si&c=14&ex=css&jc=style&f0=

    when it should be:
    http://darkentity-static.info/index.php?RF=DARK&loc=ge&c=si&ex=15&jc=css&f0=style

    Can't figure out why, for some reason the regex is not martching the "DARK" in the URL.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    Try changing those *s to +s. If the URL it tries to match starts with a slash then your first [^/]* will match nothing (it's optional after all) and the rest of your groups will be pushed down by one.
    But you also don't have enough groups: five there when you need six.

    In general you should try to be specific with expressions. Is that "14" always a number? Is "css" always a particular thing, like "css" or "js" or "images"? How about that "style"?
    Code:
    RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ /index.php?RF=$1&loc=$2&c=$3&ex=$4&jc=$5&f0=$6 [L,QSA]
    Also:
    - It's good practice to anchor the URL at the beginning and end unless you really don't care where in the URL it matches.
    - I use ^/? because in some setups the URL may start with a slash and in others it may not. Your setup looks like it does so you could just keep ^/.
    - Your [R] flag will redirect the user to the new URL. You sure you actually want that?
    - The [L] flag is another good thing to use. Without it, Apache and mod_rewrite will keep on testing, matching, and replacing according to other patterns you might have. [L] tells it to stop.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    I was using the R flag for testing purposes, to see where it was trying to go. Will remove on the production version.

    Yes, the ex variable (in this case with the value of 14) will always be a digit.

    The jc variable (in this case with the value of css) will always either be css or js. I guess, in that instance, I could do "css|js" as the match, instead of "^/".

    BTW, that rewrite rule you posted works perfectly, thanks.

    I modified it to this:
    Code:
    ^/?([^/]+)/([^/]+)/([^/]+)/(\d+)/([css|js]+)/([^/]+)/?$
    Seems to be working, but just thought I double check to make sure that's right.
    Last edited by HDFilmMaker2112; February 26th, 2013 at 11:50 PM.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    Almost. []s are for character sets: any character you put in there will match. Since you put "css|js" with a + it'll match any number of "c", "s", "|" (which loses its special meaning in character sets), and "j" characters. Like "jscjcscjsjcjcss".

    Use a simpler
    Code:
    /(css|js)/
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    Works excellent. Thanks a ton.

IMN logo majestic logo threadwatch logo seochat tools logo