URL Mod Rewrite Help

Discussion in 'Apache' started by lv211, Jan 14, 2008.

  1. #1
    I'm trying to rewrite a url from the following.

    The URL looks like this www.site.com/page?name=Digital+Point+Forums

    I want to change it to /directory/Digital-Point-Forums

    I'm not sure how to do this. This is what I got so far. I can't figure out if it's possible or not to substitute the + for the hyphens.

    RewriteRule ^/directory/(.*) page?name=$1 [L]
     
    lv211, Jan 14, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    
    RewriteRule ^directory/([^-]+)-([^-]+)-([^-]+)-([^-]+)-([^-]+)$ page?name=$1+$2+$3+$4+$5
    RewriteRule ^directory/([^-]+)-([^-]+)-([^-]+)-([^-]+)$ page?name=$1+$2+$3+$4
    RewriteRule ^directory/([^-]+)-([^-]+)-([^-]+)$ page?name=$1+$2+$3
    RewriteRule ^directory/([^-]+)-([^-]+)$ page?name=$1+$2
    RewriteRule ^directory/(.*)$ page?name=$1
    
    Code (markup):
    That should catch each word with the hyphen (-), and send it out with a plus (+). It, as you can see, only supports up to 5 hyphens (catching each URL in turn). It's untested, but I'm sure you can use it as a base for something brilliant. Just my theory.

    Jay
     
    jayshah, Jan 14, 2008 IP
    lv211 likes this.
  3. lv211

    lv211 Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Jay.

    That code isn't working for me. I'm playing around with it to get it to work.

    I've been trying \w+ and other word combinations.

    No luck yet though.


    Thanks.
     
    lv211, Jan 14, 2008 IP
  4. lv211

    lv211 Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok. I got it working now.

    I did three things.

    1. I changed ([^-]+) to (.*) .

    2. I took the /directory/ part out. I found that the first match would try to grab the directory part and I couldn't control it.

    3. I had a previous line in my .htaccess file that was very similar. So I changed the file extension from .html to .htm.

    This seems to work for me.

    Thanks.
     
    lv211, Jan 14, 2008 IP