301 redirect to absolute address

Discussion in 'Apache' started by fb-guy, Dec 19, 2006.

  1. #1
    I am trying to redirect a series of php address to a single absolute address. Something like: mysite.com/cookbooks/free.php?in=us&asin=B0000645E8 to mysite.com/

    My .htaccess is:

    RedirectMatch /cookbooks/(.*)\.php$ mysite.com/index.html (I took the http://www. out because I can't post live links)

    Which redirect to "mysite.com/index.html?in=us&asin=B0000645E8.

    How do I get rid of the training code in the URL?
    Thanks.
    FB
     
    fb-guy, Dec 19, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I send it to a page called redirect.php which then uses a simple on page 301 redirect to send it to the correct location.

    Seems like a strange way around it but at the time it was quicker than trying to figure out the solution. :)
     
    mad4, Dec 19, 2006 IP
  3. fb-guy

    fb-guy Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Would that have the same issus as my rewrite -- where the end of the link is pass through to the redirect destination?
    FB
     
    fb-guy, Dec 19, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No. This will work fine.
     
    mad4, Dec 19, 2006 IP
  5. Zulu

    Zulu Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This method (mod_alias) is not suitable for such redirects. Redirect or RedirectMatch (directives provided by mod_alias) are used to subsitute the host and initial directories (matched). It automatically appends the rest of the path and the query string to the target url. The correct method is to use mod_rewite. Use this code instead:

    RewriteEngine  On
    
    RewriteRule  ^/cookbooks/.*\.php.*$ http://www.mysite.com/index.html
    Code (markup):
    It would not append anything to the target url. The matching regexp can be improved. I hope that helps.

    PS: You can post urls (not live links though) as I did.
     
    Zulu, Dec 19, 2006 IP
  6. fb-guy

    fb-guy Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I just cannot get this to work. Any help would be appreciated. Here are my three rewrites in .htaccess. The other two see to work. Help!
    FB

    Code:

    RewriteEngine On
    RewriteRule ^/cookbooks/.*\.php.*$ http://www.mysite.com/index.html

    # REWRITE RULES
    Options +SymlinksIfOwnerMatch -Indexes
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
    RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
    </IfModule>

    RewriteEngine On
    RewriteRule ^((urllist|sitemap_).*\.(xml|txt)(\.gz)?)$ forum/vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 [L]
     
    fb-guy, Dec 20, 2006 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    
    RewriteRule ^/cookbooks/(.*)$ http://www.mysite.com/
    
    Code (markup):
     
    krakjoe, Dec 20, 2006 IP
  8. Zulu

    Zulu Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If you set a RewriteBase, the match pattern should not start with a slash (/). Perhaps, you dont know much about url rewriting. The line of code I posted above needs some rewrite flags appended to it ([L]) if there are more rules there. Here is your full htaccess code with external redirect for /cookbook/*.php, tested and working. Just copy and paste without any modification.

    
    Options +SymlinksIfOwnerMatch -Indexes
    
    # REWRITE RULES
    <IfModule mod_rewrite.c>
    
        RewriteEngine on
        RewriteBase /
    
        RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
        RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
    
        RewriteRule ^cookbooks/.*\.php http://www.mysite.com/index.html? [R=301,L]
    
        RewriteRule ^((urllist|sitemap_).*\.(xml|txt)(\.gz)?)$ forum/vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 [L]
    
    </IfModule>
    Code (markup):
    Dont remove the trailing question mark (?) for the substitution url (http://www.mysite.com/index.html?), it does the trick for stipping out any query string if there is one. I forgot to include this in my post above. Cheers!

    - Zulu
     
    Zulu, Dec 20, 2006 IP
  9. fb-guy

    fb-guy Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Zulu,

    That did the trick. Thanks! I have a lot to learn.
    Enjoy your holidays.
    FB
     
    fb-guy, Dec 20, 2006 IP
  10. Zulu

    Zulu Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You are welcome. We all are here to learn ourselves and help others learn :)

    - Zulu
     
    Zulu, Dec 20, 2006 IP