Redirect links to external sites using .htaccess

  1. quotations profile image95
    quotationsposted 2 years ago

    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)

  2. pauldeeds profile image
     
    87
    pauldeedsposted 2 years ago

    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

 
The post you are replying to is over two months old. Please DO NOT reply UNLESS you have truly new information to add to this conversation. Consider creating a new forum thread instead.
Some errors were found:

Advertising and promotional posts in the forums will be deleted and may result in the closing of your HubPages account. Signatures are also prohibited. forum rules

Check the HubPages FAQ before asking HubPages related questions.


You are sending a private message to the HubPages team


working