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
    #621
    Do you know how to edit the script so it includes the site name in the URL?

    is view_video.php in domain.com/view/view_video.php or domain.com/view_video.php ? I'm guessing .htaccess is at domain.com/.htaccess See if it works if .htaccess only has

    Options +FollowSymLinks +Indexes
    RewriteEngine On
    RewriteBase /
    RewriteRule ^view/([^.]+)/([^.]+)/([^.]+)\.html$ view_video.php?viewkey=$1&page=$2&viewtype=&category=$3 [L]
    RewriteRule ^view/([^.]+)/([^.]+)\.html$ view_video.php?viewkey=$1&page=$2&viewtype=&category= [L]
    RewriteRule ^view/([^.]+)\.html$ view_video.php?viewkey=$1 [L]

    You can't redirect to another page and make the new URL keep showing the old page. Just link to http://subdomain.domain.com/dir/dir/dic if it's an actual page.
     
    Nintendo, May 21, 2007 IP
  2. davcheong

    davcheong Well-Known Member

    Messages:
    1,148
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    175
    #622
    Yes i know how to edit the script. I need to know the .htaccess edit, not any other script. Would you tell me would the below do? Or do i miss something.

    Thanks in advance.
     
    davcheong, May 21, 2007 IP
  3. mipa jim

    mipa jim Peon

    Messages:
    261
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #623
    I have revamped a section of my site contained in one directory and moved to a completely different script in another directory to run it.

    Now after several weeks of work I want to take the new part live but can't seem to get first part of redirects to work

    The original URL's look like this

    domain.com/community/recipes/viewrecipe.php?id=3113

    Now in the new directory that same page has a URl that looks like this.

    domain.com/cooking/recipe/3113/cauliflower-in-tomato-sauce.html

    Now after following this great guide
    I have an htaccess in the original directory that looks like this

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^/cooking/recipe/(.*)/(.*)\.html$ /community/recipes/viewrecipe.php\?id=$1 [R=301,L]


    So, of course after several tries, several changes, I still can't find a way to get it to work for some reason.

    I would more than seriously appreciate a solution to my continued ignorance and an end to be the frustration
     
    mipa jim, May 22, 2007 IP
  4. 330td

    330td Well-Known Member

    Messages:
    856
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    135
    #624
    Yeah there is some good things here.

    I didn't know the existence of this thread before :)
     
    330td, May 22, 2007 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #625
    RewriteRule ^detail/(.*)/(.*)\.html$ detail.php?link=$1 [L]

    if detail.php?link=884 is the original URL.

    RewriteRule ^community/recipes/viewrecipe.php\?id=(.*)$ cooking/recipe/$1/$2 [R=301,L]

    Except there's no way you can redirect it cause it'll have no idea what $2 is.

    All you can do is link to

    domain.com/cooking/recipe/3113/cauliflower-in-tomato-sauce.html

    RewriteRule ^cooking/recipe/(.*)/(.*)\.html$ community/recipes/viewrecipe.php?id=$1 [L]
     
    Nintendo, May 22, 2007 IP
  6. davcheong

    davcheong Well-Known Member

    Messages:
    1,148
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    175
    #626
    Yes it set to detail.php?link=884. But it seem still cant work, even on a testrun website. Not sure whether i miss place it here :

    Options -Indexes
    Options +FollowSymLinks
    DirectoryIndex index.php index.html
    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-l
       RewriteRule ^(.*)$ index.php [QSA,L]
       Rewritecond %{http_host} ^domain.com [nc]
       Rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
       RewriteRule ^detail/(.*)/(.*)\.html$ /detail.php?link=$1 [L]
    </IfModule>
    
    Code (markup):
    Did i put it in the correct place? Cos when hover on the link or clicking on the link its still showing detail.php?link=123
     
    davcheong, May 23, 2007 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #627
    You have to edit the script to link to the new URLs.
     
    Nintendo, May 23, 2007 IP
  8. LittlBUGer

    LittlBUGer Peon

    Messages:
    306
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #628
    Hello again. Is there any way to be able to use RewriteCond as well as the rules with this code on my site? I've been trying so many different combinations left and right with just the RewriteRule and nothing has worked, yet when I've began experimenting with the RewriteCond on top of it (using Query_String or whatever), I can sometimes get my server to give an Internal Error message, so I know it's reading the .htaccess file and whatnot, I'm just not getting the code correct. Thanks. :)
     
    LittlBUGer, May 25, 2007 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #629
    I rarly ever use RewriteCond.
     
    Nintendo, May 25, 2007 IP
  10. Hyipgrace

    Hyipgrace Peon

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #630
    Thank you very much brother! It's really helpful to me!
     
    Hyipgrace, May 27, 2007 IP
  11. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #631
    Say you were to do this code..

    RewriteRule ^index.htm$ http://www.domain.com/ [R=301,L]
    Code (markup):
    But wanted to redirect a page to another page on the same domain and same server, would you still have to do the R=301? :confused:

    I've been doing it this whole time with just [L].. :p
     
    EGS, May 28, 2007 IP
  12. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #632
    Grrr...what is wrong with my code? :mad:

    RewriteRule ^free\-web\-directories.html$ free_web_directories.html [L]
    RewriteRule ^premium\-web\-directories.html$ premium_web_directories.html [L]
    RewriteRule ^premium\-web\-directory\-submission\-report.html$ premium_web_directory_submission_report.html [L]
    RewriteRule ^manual\-directory\-submission.html$ manual_directory_submission.html [L]
    RewriteRule ^sponsor\-directory\-lister.html$ sponsor_directory_lister.html [L]
    Code (markup):
     
    EGS, May 28, 2007 IP
  13. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #633
    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^free\-web\-directories\.html$ free_web_directories.html [L]
    RewriteRule ^premium\-web\-directories\.html$ premium_web_directories.html [L]
    RewriteRule ^premium\-web\-directory\-submission\-report\.html$ premium_web_directory_submission_report.html [L]
    RewriteRule ^manual\-directory\-submission\.html$ manual_directory_submission.html [L]
    RewriteRule ^sponsor\-directory\-lister\.html$ sponsor_directory_lister.html [L]

    You can see if you can get it working with...

    RewriteRule ^wacko\.html$ index.html [L]

    Now, go make a few more polls!!! :D:D:D:D
     
    Nintendo, May 28, 2007 IP
  14. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #634
    If you use a full URL instead of a path, the rewrite engine cannot tell that the destination is actually on the same server and still accessible so it has no choice but to send out a redirect header for the new location. The default redirect status is a 302 for temporary redirects so if you're doing SEO work, you probably want to specify 301 by explicitly using the R flag and giving it the 301 value.

    The dash isn't strictly speaking a metacharacter and so doesn't need escaping, unless of course it's positioned within a class where it's meaning could be confused.. i.e [a\-g] matches a, the dash and g, whereas [a-g] matches every letter between a and g alphabetically. But if you wanted the former, it's easier to just write [ag-]. In this instance, it can only possibly be a literal dash so the backslash is unnecessary. Whether that's your problem or not, I don't know but maybe worth trying without.
     
    rodney88, May 29, 2007 IP
  15. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #636
    Your code is all wacked if you're trying redirects!!! :D

    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^free_web_directories\.html$ free-web-directories.html [R=301,L]

    for an example
     
    Nintendo, May 29, 2007 IP
  17. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #637
    Yep, that didn't work!
     
    EGS, May 30, 2007 IP
  18. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #638
    Nintendo, May 30, 2007 IP
  19. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #639
    Dat don't work!! :mad:
    http://www.directorylister.info/free_web_directories.html is NOT being redirected to http://www.directorylister.info/free-web-directories.html

    !!!
     
    EGS, May 30, 2007 IP
  20. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #640
    Nintendo, May 30, 2007 IP