Mod rewrite and slashes

Discussion in 'Apache' started by ma0, Sep 21, 2007.

  1. #1
    Hi, I need to solve this:

    site.com/word/24/7?page=2 should become something like
    /wordlist/show.php?word=24/7&page=2

    note the / !

    the part "24/7" could be anything, in fact I already have good rules but they do not work with slashes.
    This is my last attempt:
    RewriteRule ^words/(.+)$ /wordlist/show.php?word=$1 [L]
    (where page=2 is sent with the html link)
    It's the whole day I'm trying this and I have no idea how to solve it.
    RewriteRule ^words/(.+)\?page=2 /.....
    seems not to work either.
     
    ma0, Sep 21, 2007 IP
  2. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    shouldn't be that hard

    ([a-zA-Z0-9]+[/]?)

    try that for the 24/7 , going off the top my hard, but think it should be what you are looking for
     
    powerspike, Sep 21, 2007 IP
  3. evera

    evera Peon

    Messages:
    283
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah someting like that:

    RewriteRule ^words/([a-zA-Z0-9\-/]+)\?page=(.*)$ /wordlist/show.php?word=$1&page=$2 [L]
    Code (markup):
     
    evera, Sep 21, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    ma0 and evera, you cannot match the query string in the RewriteRule pattern.

    Use the QSA flag to append the query string to the rewritten URL:
    RewriteRule ^words/(.+)$ /wordlist/show.php?word=$1 /wordlist/show.php?word=$1 [QSA,L]

    That rewrites /words/test?page=2 , /wordlist/show.php?word=test&page=2

    But, while you are already using mod_rewrite, why not make the page links friendly too?
    RewriteRule ^words/(.+)(/page([0-9]+))?$ /wordlist/show.php?word=$1&page=$3 /wordlist/show.php?word=$1 [L]

    That matches /words/test , /words/test/page2 etc.
     
    krt, Sep 21, 2007 IP
  5. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Krt, I haven't checked your code yet, but my problem is that some words contains slashes "24/7" can't be used with simple rewrite rules.
    I would love to be able to do so, but how can Apache recognize
    /word/myword/2 (or /word/myword/page2 as you said) and
    /word/24/7/2 ? I should probably have something complicated like a "lastIndexOf("/") to find out the last / and use the slashes before in the words.

    I wanted to SEOize it but if I can't, I think I will probably have to do a search on all my words and change the / with something different.

    Edit:
    Thinking about it, I think I need to find a way to be able to use /word/24/7/2 or find a substitute for the /, otherwise google will index only my first page.
    HELP :)
     
    ma0, Sep 22, 2007 IP
  6. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #6
    That's why I used "page#" instead of just "#"

    I don't think .htaccess alone would be enough to identify 24/7 as an item instead of the 7th page of the item "24".

    This could be solved by using ref's for your content items with special characters and spaces that are ill suited to URLs substituted. I use a hyphen for this.

    Anyway, back to what I was saying, using page# means the rewrite rule I posted will differentiate properly, e.g.
    /words/24/7 will go to ...?word=24/7
    /words/24/7/page2 will go to ...?word=24/7&page=2
     
    krt, Sep 22, 2007 IP
  7. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I should try the other road.. I want google to see all my pages.
    Maybe I should try the hyphen thing.
    The problem is that I have no idea which character is NOT used by all the words. I could have words with ~, / ..even ? ..
    I was thinking to urlencode them, but that doesn't solve the problem.
     
    ma0, Sep 22, 2007 IP
  8. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #8
    Google will see all the pages using my method and will have less problems than using query strings.

    As for the other issue, I mean this: have a reference version for each word, e.g:
    ref | word
    apple | apple
    24-7 | 24/7
    ping-pong | ping-pong
     
    krt, Sep 22, 2007 IP
  9. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I don't get what you mean with reference. where do you have it? I have no idea on what word I'll be using. That's the problem.
    And I could have the word 24/7 and the word 24-7 too.
     
    ma0, Sep 24, 2007 IP
  10. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #10
    I meant a different version of the word without any special characters that references the actual word. Just an idea though.
     
    krt, Sep 24, 2007 IP
    ma0 likes this.
  11. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks for the ideas.
    I will try them tonight.

    The references will never work though. Look at my blog to see what I'm doing and you'll see why.
    I do not decide which word exists or not, so I could have 247 24-7 24/7 24?7 on the Db.
     
    ma0, Sep 24, 2007 IP
  12. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    KRT,sorry for the delay.
    I tried today and your .httacces rule doesn't work.
    It takes the whole word AND /pageX as a word

    so I get word="myword/page2" and page=""

    Apparently there isnt a solution for that. :(
     
    ma0, Oct 4, 2007 IP
  13. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #13
    Sorry, I have been absent from these forums for the last 2 to 3 weeks.

    There is a solution as shown here, the problem was a missing ? to signify ungreedy behaviour.

    RewriteRule ^words/(.+?)(/page([0-9]+))?$ /wordlist/show.php?word=$1&page=$3 /wordlist/show.php?word=$1 [L]
     
    krt, Oct 19, 2007 IP
  14. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I will try it then. Are you sure of what you write? I've never seen a RewriteRule with two "destination" (how do you call the second part of the rule?)
     
    ma0, Oct 22, 2007 IP