FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

Discussion in 'Apache' started by Nintendo, Jul 30, 2005.

  1. jim_h

    jim_h Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #241
    Just getting into working with mod_rewrite and this thread has been incredibly helpful so far. I'm having some trouble with my own (first attempt) url rewriting. I've got some bigger plans to make but I figured I'd start simple, especially since the change I'm trying to make is relevant.

    I've got a music review site and I've recently changed the link scheme from review.php?reviewID=* to review.php?type=album&reviewID=* where the IDs are numbers. This change was to accomodate different types of reviews and after reading about mod_rewrite I plan to make these urls less ugly and more memorable. But as it stands now I have a record label that I've done reviews for who has linked to the reviews on their own site.

    The problem is theyve used the old link scheme, and I'm worried about search engines still crawling the old links. To save them the cash on an out of schedule update I've rigged it so that review.php?reviewID will still point to album reviews, and place that amidst the new link scheme/php layout. However I'd like to cut this off altogether and use the new link scheme, without causing them to change their links.

    The rule I've set is:

    RewriteRule ^review\.php?reviewID=([0-9]+)$ review.php?type=album&reviewID=$1 [R=301,L]

    I've toyed around with the [R=301,L] portion a bit as well. I'm running into 2 problems:

    The old links are still showing up in the address bar and I'm worried that search engines will crawl them as such.

    This for whatever reason has cut off the logo at the top of the page, I think that might just be a relative scripting thing and am checking on that now. If someone could take a look at that and see where I've gone wrong it'd be really helpful. Thanks
     
    jim_h, Feb 25, 2006 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #242
    RewriteRule ^review\.php?reviewID=([0-9]+)$ review.php?type=album&reviewID=$1 [R=301,L]

    looks like you're redirecting to a more dynamic URL. If you change the URLs, you want static URLs, and you can't both change a URL, and do a 301 redirect at the same time.

    RewriteRule ^review/([0-9]+)/$ review.php?type=album&reviewID=$1 [L]

    Enter the full URL in the image code for the logo.
     
    Nintendo, Feb 25, 2006 IP
  3. jim_h

    jim_h Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #243
    i guess i should just bypass the middleman i was going through for redirecting to the new url scheme and go static all around. thanks for the help! i guess the [L] is what sticks the friendlier url in the address bar

    another question: if i rewrite urls from dynamic to static with faux-subdirectories like from review.php?id=2 to reviews/2/, and someone enters reviews/4/ instead of clicking on review.php?id=4, would it parse that page correctly as if it were review.php?id=4. and is it good practice to switch all links coded into the site to the new simpler format or can i continue using url parameters

    i guess im just confused as to what rewriting urls actually does for the end user or an SE bot, i'll browse around the forum some more

    edit: alright i guess i was ass-backwards. understanding it better and i'll give it another go. thanks nintendo for the leg-up
     
    jim_h, Feb 25, 2006 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #244
    reviews/4/
    would be the exact same thing as
    review.php?id=4

    both URLs work.
     
    Nintendo, Feb 25, 2006 IP
  5. jim_h

    jim_h Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #245
    finally got the hang of it and am currently editing my php pages to point to the new links. wanted to say thanks again for the effort helping everyone out (this goes to everyone else as well).
    when i first got wind of mod_rewrite a few weeks back and took a look at what was involved it looked far too daunting. feelin a lot more confident and optimistic about it now that i have some kind of idea what im doing.
     
    jim_h, Feb 25, 2006 IP
  6. rederick

    rederick Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #246
    rederick, Mar 21, 2006 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #247
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^directory/subdirecory/([^.]+)\.aspx$ http://www.domain.com/$1/ [R=301,L]
    RewriteRule ^directory/filename.html$ http://www.domain.com/ [R=301,L]
     
    Nintendo, Mar 21, 2006 IP
  8. gUrLaLiEn

    gUrLaLiEn Peon

    Messages:
    1,235
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #248
    omg... this forum answered most of my questions. thanks nintendo! ;)
     
    gUrLaLiEn, Mar 23, 2006 IP
  9. mgrohan

    mgrohan Active Member

    Messages:
    671
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    58
    #249
    Big thanks for this thread. Have been searching the net for ages for some decent info on mod rewrites. Finally found it!! :)

    Now t0 give it a try ..
     
    mgrohan, Apr 7, 2006 IP
  10. acidfrenzy

    acidfrenzy Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #250
    This is good info but I guess one should have done this when starting the webpage. Not a couple of months old one like mine. I made a redirect from non-www to the www version and lost pretty much all traffic from google. I thought it would not lose any traffic because all my incoming links is to the www address.

    What's the reason for this to happen?

    It only affected google searchengine. On MSN and other searchengines I'm at the top as I was on google before the 301.
     
    acidfrenzy, Apr 10, 2006 IP
  11. maildeepak

    maildeepak Peon

    Messages:
    220
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #251
    hai Nintendo... thanx for the great tutorial....after reading this i managed to get the www and non-www issue sorted out...:)

    but when i tried to redirect index.html to www.domain.com/ It did not work out. the firefox browser says that the redirection request could not be completed. this is the code i have in my htaccess modelled on what you have given above...;)

    my idea behind this code segment is to redirect domain.com, domain.com/index.html and www.domain.com/index.html to www.domain.com

    in the above code, the first part works i.e. non-www to www code...but the second segment doesn't . what am i doing wrong here?

    thanx for your guidance..

    cheers
    deepak.c
     
    maildeepak, Apr 10, 2006 IP
  12. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #252
    I'm guessing it's an infinite loop, from .com/ and .com/index.html using the same file.
     
    Nintendo, Apr 11, 2006 IP
  13. maildeepak

    maildeepak Peon

    Messages:
    220
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #253
    i dont understand this .....suppose like you said both are using the same file, then how come:

    www.domain.com is having a PR of 5 and cached on 7 april 06 while
    www.domain.com/index.html is not having any pr and is also cached on same date.
     
    maildeepak, Apr 11, 2006 IP
  14. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #254
    Googles not the same as apache!!!
     
    Nintendo, Apr 11, 2006 IP
  15. maildeepak

    maildeepak Peon

    Messages:
    220
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #255
    then what shall i do now...

    basically now i have two sites with similar content...

    www.domain.com and www.domain.com/index.html

    ur suggestions...
     
    maildeepak, Apr 11, 2006 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #256
    Never link to the URL with index.html in it. Only link to .com/
     
    Nintendo, Apr 11, 2006 IP
  17. maildeepak

    maildeepak Peon

    Messages:
    220
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #257
    that is ok...inside the website i can do that. but what will happen if google comes to find about the two website types...i.e. index.html and /

    regards,
    deepak.c
     
    maildeepak, Apr 11, 2006 IP
  18. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #258
    Only way for Google to find it is through a link. I got a few vBulletin boards that have two URLs for the index and I have yet to ever get banned for duplicate content from one duplicate URL.
     
    Nintendo, Apr 11, 2006 IP
  19. maildeepak

    maildeepak Peon

    Messages:
    220
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #259
    so your suggestion is to ignore completely the index.html and conc. only / so that google automatically ignores the index file.....

    ok...

    i will start working on that...

    regards,
    deepak.c
     
    maildeepak, Apr 11, 2006 IP
  20. mgrohan

    mgrohan Active Member

    Messages:
    671
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    58
    #260
    Nintendo, thanks for the helpful thread. I have read all the way through it and testing every combination to get the rewrites to work without success.

    I need:
    http://www.my site.com/view_offer.php?id=71

    to go to:
    http://my site.com/viewoffer-71.html

    Similarly:
    http://www.my site.com/selloffers.php

    to go to:
    http://www.my site.com/selloffers.htm

    My .htaccess currently shows (not including my failed attempts at the above):

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on

    RewriteCond %{HTTP_HOST} !^www\..* [NC]
    RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]

    RedirectMatch permanent ^/gen_confirm.php$ http://www.my site.com

    Anyhelp would be greatly appreciated, thanks!
     
    mgrohan, Apr 13, 2006 IP