Redirect links to external sites using .htaccess

  1. quotations profile image96
    quotations
    110 posts
    Joined: 2 years ago
    Hubs: 33
    Fans: 260

    Hi Everyone,

    I am hoping that one of my fellow hubbers can help me with a coding problem. I would like to use htaccess mod_rewrite to change a large number of links (each with some variation) to a new external site. There are too many links to handcode and too many variables to do a search and replace.

    Here is what I would like to do:

    Current links examples:

    http://www.example.com/1.html
    http://www.example.com/2.html
    http://www.example.com/3.html

    etc.

    Would now be rewritten:

    http://www.newsite.com/

    The rewrite would be at the server level and the original html would remain.

    So that all links, regardless of the original url all point to the same page on the new site. I know I need to use wildcard variables to allow for the fact that the url pointing to example.com has different html after the.com but I have not been able to get it to work and when I googled this subject I couldn't find a site that answered my question.

    Thanks in advance for any help you can give me.

    (ps. I am using an apache server)

    Posted 2 weeks ago
  2. pauldeeds profile image
     
    85
    pauldeeds
    1826 posts
    HubPages Staff
    Joined: 3 years ago
    Hubs: 1
    Fans: 374

    A rule like this in the .htaccess on the example.com server will 301 redirect any URL on example.com to the same URL at newsite.com.  For instance:

    RewriteRule (.*) http://newsite.com/$1 [R=301,L]

    http://example.com/foo.html -> http://newsite.com/foo.html

    If you need to match certain urls and rewrite them to something else on the new server, just change the rules.  For instance:

    RewriteRule ^foo/(.*).html$ http://newsite.com/bar/$1.php [R=301,L]

    http://example.com/foo/blah.html -> http://newsite.com/bar/blah.php

    Posted 2 weeks ago
 
working