FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

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

  1. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #161
    That's easy!

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^travel\-shop/([^.]+)\.html$ travel/travel-services.php?travel_service=$1 [L]
     
    Nintendo, Dec 17, 2005 IP
  2. classifieds

    classifieds Sopchoppy Flash

    Messages:
    825
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    150
    #162
    Nintendo,

    I've read through most of the posts and seen exact examples of what I'm trying to do but I still can't get it to work.

    I curently have some modrewrites in .htaccess so I know the module is working. . .

    Here is what I'm trying to do and along with some examples:

    Attempt #1: URL www. domain .com/modules.php?mod=Auto_Notify
    RewriteRule ^auto-notify\.html$ modules.php?mod=Auto_notify [L]


    Attempt #2: URL www. domain .com/modules.php?mod=Auto_Notify
    RewriteRule ^tools/(.*)\.html$ modules.php?mod=$1 [L]
    (trying to make: .com/tools/Auto_notify.html )


    Attempt #3: www. domain . com/modules.php?mod=Extra_Pages&pg=partnerlinks
    RewriteRule ^resources/(.*)\.html$ modules?mod=Extra_Pages&pg=$1 [L]
    (trying to make: .com/resources/partnerlinks.html )
     
    classifieds, Dec 19, 2005 IP
  3. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #163
    If you're trying all three of them at the same time,

    auto-notify should be auto\-notify

    and in order it should be Attemt 3, 2, 1. Post the complete .htaccess code.
     
    Nintendo, Dec 19, 2005 IP
  4. classifieds

    classifieds Sopchoppy Flash

    Messages:
    825
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    150
    #164
    Nintendo,

    The three examples were individual attempts to get it to work. . . Here's the current htaccess that includes attempt #3 as the first rule:

    Options +FollowSymLinks
    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    
    RewriteRule ^tools/(.*)\.html$ modules.php?mod=$1 [L]
    
    
    RewriteRule ^page-([^\.]+)\.html$ index.php?page=out&id=$1
    
    RewriteRule ^(([^-]+-([0-9]+))?.*)?ad([0-9]+)\.htm$ index.php?page=out&id=$4&set_region=$3
    
    RewriteRule ^([^-]+)-r([0-9]+)\.html$ index.php?page=browse&type=region&cat=$2
    RewriteRule ^([^-]+)-rs([0-9]+)\.html$ index.php?page=browse&type=region_select&cat=$2
    RewriteRule ^([^-]+)-rsr([0-9]+)\.html$ index.php?page=browse&type=region_select&cat=$2&return=yes
    
    Code (markup):
     
    classifieds, Dec 20, 2005 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #165
    I'm not sure about the

    RewriteRule ^(([^-]+)\-([0-9]+))?.*)?ad([0-9]+)\.htm$ index.php?page=out&id=$4&set_region=$3 [L]

    that looks messed up.

    And it looks like you changed the URL to add something in it that's not in the original URL. Hince starting with $2 instead of $1, or $3 and $4.


    <IfModule mod_rewrite.c>
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^tools/(.*)\.html$ modules.php?mod=$1 [L]
    RewriteRule ^page\-([^\.]+)\.html$ index.php?page=out&id=$1 [L]

    RewriteRule ^(([^-]+)\-([0-9]+))?.*)?ad([0-9]+)\.htm$ index.php?page=out&id=$4&set_region=$3 [L]

    RewriteRule ^([^-]+)\-rsr([0-9]+)\.html$ index.php?page=browse&type=region_select&cat=$2&return=yes [L]
    RewriteRule ^([^-]+)\-rs([0-9]+)\.html$ index.php?page=browse&type=region_select&cat=$2 [L]
    RewriteRule ^([^-]+)\-r([0-9]+)\.html$ index.php?page=browse&type=region&cat=$2 [L]
     
    Nintendo, Dec 20, 2005 IP
  6. classifieds

    classifieds Sopchoppy Flash

    Messages:
    825
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    150
    #166
    All the rules execpt for the first one are working. . . . . . which is why its so frustrating.

    The changes I want to make seem relatively simple but for some reason it's not :(

    -jay
     
    classifieds, Dec 20, 2005 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #167
    If

    RewriteRule ^tools/(.*)\.html$ modules.php?mod=$1 [L]

    doesn't work, but

    RewriteRule ^page\-([^\.]+)\.html$ index.php?page=out&id=$1 [L]

    does, try

    RewriteRule ^tools/([^.]+)\.html$ modules.php?mod=$1 [L]

    (A \ doesn't need to be in ([^.]+) )
     
    Nintendo, Dec 20, 2005 IP
  8. classifieds

    classifieds Sopchoppy Flash

    Messages:
    825
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    150
    #168
    I made the changes and it does not work - see below.

    Question. . this is a test site in a directory off of a production domain i.e., domain. com/test-site/ . I copied the .htaccess from the web root into /test-site/ and have been making the modifications there. I was operating under the assumption that the .htaccess in the lowest level directory took precedence (sp?) . Could this be causing the problem?

    Thanks!

    -jay


    
    Options +FollowSymLinks
    
    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    #
    RewriteRule ^tools/([^.]+)\.html$ modules.php?mod=$1 [L]
    
    RewriteRule ^page-([^\.]+)\.html$ index.php?page=out&id=$1
    
    RewriteRule ^category-19.html  http://www.classifiedads.com/sports_equipment-19.html  [R=permanent]
    
    
    #RewriteRule ^([^-]+)-([0-9]+)\.html$ index.php?page=browse&cat=$2
    #RewriteRule ^([^-]+)-([0-9]+)_([0-9]+)\.html$ index.php?page=search&s_res=AND&cid=$2&off=$3
    
    Code (markup):
     
    classifieds, Dec 21, 2005 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #169
    It's missing

    RewriteBase /

    and I never need
    <IfModule mod_rewrite.c>

    I usually use domain.com/.htaccess. I'm not sure exactly what it does when there are both root and directory ones at the same time.
     
    Nintendo, Dec 21, 2005 IP
  10. Dakait

    Dakait Banned

    Messages:
    287
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #170
    I need 301 redirect, implimented with .htaccess file.

    I have lot of files with space in their name. Example - "search engine optimization.htm" need to redirect to "search-engine-optimization.php".

    Can you tell me how to do it, please?
     
    Dakait, Dec 21, 2005 IP
  11. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #171
    Edit the php file to replace the spaces with _ or -.
     
    Nintendo, Dec 21, 2005 IP
  12. Dakait

    Dakait Banned

    Messages:
    287
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #172
    This is alternate... Actually I am getting lot of traffic on pages with space in their name, so i can't change their name and then redirect it.

    Also, if i replace space with _ or - then it simply means...change in file name...which could be with .php also :)
     
    Dakait, Dec 21, 2005 IP
  13. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #173
    I don't think it'll work!

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+) ([^.]+) ([^.]+).html$ http://www.domain.com/$1-$2-$3.htm [R=301,L]
    RewriteRule ^([^.]+) ([^.]+).html$ http://www.domain.com/$1-$2.htm [R=301,L]
     
    Nintendo, Dec 21, 2005 IP
  14. DarrenC

    DarrenC Peon

    Messages:
    3,386
    Likes Received:
    154
    Best Answers:
    0
    Trophy Points:
    0
    #174
    DarrenC, Dec 22, 2005 IP
  15. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #175
    I think you have to do the [R=301,L] stuff to redirect the PR.
     
    Nintendo, Dec 22, 2005 IP
  16. DarrenC

    DarrenC Peon

    Messages:
    3,386
    Likes Received:
    154
    Best Answers:
    0
    Trophy Points:
    0
    #176
    Nintendo,

    So I've got this right my current .htaccess file says

    RewriteEngine on
    RewriteBase /
    RewriteRule ^([0-9]+) travel-shop/carrentals.php?id=$1

    Should it be as above but after travel-shop/carrentals.php?id=$1 [R=301,L]

    What impact would the [R=301,L] have?
     
    DarrenC, Dec 22, 2005 IP
  17. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #177
    It redirects to the new URL. I've tred it once and on the next PR update the new URL had the same PR as the old URL had.
     
    Nintendo, Dec 22, 2005 IP
  18. Dekker

    Dekker Peon

    Messages:
    4,185
    Likes Received:
    287
    Best Answers:
    0
    Trophy Points:
    0
    #178
    Any more tips on optimizing Apache and MySQL? :D
     
    Dekker, Dec 22, 2005 IP
  19. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #179
    I've never figured out how to Optimize mySQL. For Apache I've never had to do any more than those number settings.

    RewriteEngine on
    RewriteBase /
    RewriteRule ^([0-9]+)$ http://www.domain.com/travel-shop/carrentals.php?id=$1 [R=301,L]
    or
    RewriteRule ^travel-shop/carrentals.php?id=([0-9]+)$ http://www.domain.com/$1 [R=301,L]

    RewriteRule ^OLD-URL-PATH, no domain$ NEW COMPLETE URL $1 [R=301,L]
     
    Nintendo, Dec 22, 2005 IP
  20. Dakait

    Dakait Banned

    Messages:
    287
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #180
    I have changed the server and converted the static site into .php site.

    Also now i am not using www in doamin.

    While doing this I lost PR. It was PR6, now it is PR0.

    What to do to gain back lost PR???
     
    Dakait, Dec 27, 2005 IP