Search code examples
phpapache.htaccesshttp-redirecthttp-status-code-301

Redirect all pages of one TLD + a single directory to the root of another TLD


In Redirect all pages of one TLD to another there's a clear example of how to Redirect from the root of one TLD to the root of another TLD. But I want add a Rewrite Condition & Rule to my .htaccess file that will do the following redirects...

www.example.com/one  -->  www.example.co.uk
www.example.com/one/two  -->  www.example.co.uk/two
www.example.com/one/two/three  -->  www.example.co.uk/two/three

So basically it will redirect the URL to one where the TLD is changed and the first directory in the path (/one) is removed.


Solution

  • So something like this should be in your vhost for www.example.com

     RewriteEngine On
     RewriteRule ^/one(.*) www.example.net/$1 [R=301,L]
    

    R=301 causes the browser to cache the answer and send the next request to that target directly to the according URL. So clear your cache while testing ;)

    Or, if you don't want to see the requestor that he is being redirected:

     RewriteRule ^/one(.*) www.example.net/$1 [P,L]
    

    Which requires the mod_proxy module to be activated.


    One a sidenote: plz use example.com in examples, because domain can't be used ;)