1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mod_rewrite and redirects

Discussion in 'Apache' started by jlawrence, May 13, 2005.

  1. #1
    Hi,
    I've got an old site which I'm in the process of sorting the internal linking out on.
    The old url looked like index.php?topic=xxxx
    The new url looks like topic/xxxx

    So I put a rewriterule in for this:
    RewriteRule topic/(.*)$ /index.php?topic=$1 [L]

    This works fine, but the old urls are in various links and cached on the SEs, so I want to redirect the old to the new.

    I thought the following would do the trick:
    RewriteRule topic/(.*)$ /index.php?topic=$1 [L]
    RedirectMatch 301 index.php?topic=$1 http://domainname/topic/$1

    However, the 301 seems to be doing nothing.
    I don't know if what I'm trying to do is even possible without causing a loop of somekind.
    Anyone any views/ideas ??

    I've also tried RewriteRule index.php?topic=(.*)$ /topic/$1 [R=301, L]
    but that gave bad flag deliminator errors.


    J
     
    jlawrence, May 13, 2005 IP
  2. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #2
    getting rid of the space in the [R=301, L] so it reads [R=301,L] got rid of the flag error - but it still seems to do nothing.
     
    jlawrence, May 13, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    RedirectMatch is not associated with RewriteRule in any way.

    The rest of these, you don't have the correct regex in place. Question mark is a special character. What you expression will match is this (? makes 'p' as an optional character):

    index.phptopic=...
    index.phtopic=...

    Escape question mark (as well as other characters - periods, parenthesis, etc) with a slash:

    index.php\?topic=(.*)$

    J.D.
     
    J.D., May 13, 2005 IP
  4. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #4
    didn't mean to say that they were.


    I managed to work that out :)

    Escaping characters like ? doesn't make any difference.
     
    jlawrence, May 13, 2005 IP
  5. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's how you need to do this anyway. If you are sure that your mod_rewrite works in general, check if you have conflicting .htaccess files in the directory you are trying to access or any other directory above it, besides the one that has .htaccess you are editing.

    J.D.
     
    J.D., May 13, 2005 IP
  6. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #6
    lets just make sure I'm being clear on what I want to do.
    I want to do a 301 redirect on a url like /index.php?topic=xxxx to /topic/xxxx where xxx could be any number of things.
    I would have thought that the only things that need escaping are the ? and perhaps the =
    anyways, to try and avoid any escaping problems, I thought perhaps
    RewiteRule index.php(.)topic(.)(.*)$ /topic/$3
    but that also has no effect. I'm working on a .htaccess in the root folder (there are no other folders).
     
    jlawrence, May 13, 2005 IP
  7. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #7
    fk'it this is giving me a headache.
    I'll just write off current IBL's, wait for the site to be spidered again and then start a new link campaign.
    There's only about 40 IBL's that I know of, so it's only a day or so's work to replace them.
     
    jlawrence, May 13, 2005 IP
  8. Epica

    Epica Well-Known Member

    Messages:
    1,007
    Likes Received:
    95
    Best Answers:
    0
    Trophy Points:
    170
    #8
    Try including a ? at the end of the rewritten string - it seems like I remember this same issue with a client a while back - the rewrite would keep failing when it seemed it should work - I guess it was still looking for the variable but adding the ? with no data after it turned out the proper result for us

    I'll try to dig up that actual string if I can find it
     
    Epica, May 13, 2005 IP
  9. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This will do what you need:

    RewriteCond %{QUERY_STRING} ^topic=(.*)$
    RewriteRule ^index\.php$ /topic/%1? [R=301,L]

    J.D.
     
    J.D., May 13, 2005 IP
    jlawrence likes this.
  10. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yep, that did it.
    Let me see if I understand what your rewrite will do:
    if QUERY_STRING begins with topic= take everything after the topic assign it to variable and apply the rule.
    rewrite rule says if url starts with index.php rewrite it to /topic/variable redirected with a 301

    That worked and produced the 301. Now what I hoped wouldn't happen does :( it loops because of the static rewrite to the dynamic :(
     
    jlawrence, May 13, 2005 IP
  11. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #11
    sorted :D
    I've now got this
    RewriteCond %{QUERY_STRING} ^topic=(.*)$
    RewriteRule ^index.php$ /topic/%1? [R=301,L]

    RewriteRule topic/(.*)$ /index.php?jon=1&topic=$1 [L]

    Note the extra jon=1& in the final rewrite (this isn't used in any script) but it stops the rules looping :)

    Yipee, no need to disregard those links.
     
    jlawrence, May 13, 2005 IP
  12. bozboz

    bozboz Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I have been pulling my hair out on this exact same problem for about 4 hours. I dont normally reply on threads but this was a real Eureka moment so thanks very much!

    regular expressions is a bit of a nightmare when your just starting!
     
    bozboz, May 27, 2008 IP