Cant get it to work.. htaccess 301 redirect

Discussion in 'Apache' started by hasbehas, Mar 5, 2008.

  1. #1
    Now, I have small problem..

    I had my old page (removed now), so I want to redirect all incomings to new dir..

    I have tried ;
    RewriteRule ^my-new-dir/$ my_old_page.php$1 [R=301,L]

    why is this not working ?
     
    hasbehas, Mar 5, 2008 IP
  2. Bompa

    Bompa Active Member

    Messages:
    461
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    58
    #2

    I think you have it backwards

    RewriteRule ^/$ new_dir [R,L]

    You might need to preface new_dir with a slash on some servers.
     
    Bompa, Mar 5, 2008 IP
    hasbehas likes this.
  3. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #3
    Thanks man.. it was backwards..
    Funnything is all rest of the details in htaccess is backwards.. Or is it just 301 needs to be backwards ??


    this worked, but witha slight problem..
    RewriteRule ^my_old_file.php$ /my-new-dir/ [R=301,L]

    But this time any var after the old file comes up too..
    if my_old_file.php?con=abc is requested then I get /my-new-dir/?con=abc :(

    I also added RewriteRule ^my_old_file.php?con=abc$ /my-new-dir/con-abc.html [R=301,L]
    I still get /my-new-dir/con-abc.html?con=abc

    How Can I truncate or avoid the vars ?

    :(

    Any ideas ??
     
    hasbehas, Mar 5, 2008 IP
  4. Bompa

    Bompa Active Member

    Messages:
    461
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    58
    #4
    I'm really not sure exactly what you want in every possible situation, but try this:

    RewriteRule ^(.*)$ /new_dir/$1 [R,L]
     
    Bompa, Mar 5, 2008 IP
  5. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #5
    Well, let me put this way..

    I have links like;

    my_old_file.php
    my_old_file.php?con=abc
    my_old_file.php?con=abc&def=def

    needs redirecting to;

    my_old_file.php ==> /my-new-dir/ (works fine)
    my_old_file.php?con=abc ==> /my-new-dir/con-abc.html (cant get it work, I get :/my-new-dir/con-abc.html?con=abc)
    my_old_file.php?con=abc&def=def ==> /my-new-dir/con-abc-def.html (cant get it work)

    Can you help me now ?
     
    hasbehas, Mar 5, 2008 IP
  6. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #6
    I got it to work ;

    RewriteCond %{HTTP_HOST} (www.)?mydomain.com
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^my_old_file.php(.*)$ my-new-dir/? [R=301,L]

    The trick was the ? between www and mydomain.
    Source

    Thanks though Bompa ;)
     
    hasbehas, Mar 5, 2008 IP