Note –  browsers cache redirects – if you change a redirect and then try again your browser may not be hitting the old domain when you return!!!  Try using temporary redirect code 307 when you debug in place of 301 permanent redirect???

Redirect codes

301 – permanent redirect

307 – Temporary redirect

Redirect from old domain to new domain

RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

or to redirect to the root directory always

RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

Redirect a directory and all sub directories from an old domain to new domain

Removing the original directory from the path (with .htaccess in the root)

https://my-old-domain/my-old-directory/my-filename
redirects to:
https://my-new-domain.com/my-new-directory/my-filename

#Redirect Windows IoT site
RewriteEngine on
RedirectMatch 301 ^/my-old-directory/(.*)$ https://my-new-domain.com/my-new-directory/$1
Keeping the original directory in the path (with .htaccess in the root)

https://my-old-domain/my-old-directory/my-filename
redirects to:
https://my-new-domain.com/my-new-directory/my-old-directory/my-filename

RewriteEngine on
RewriteBase /my-old-directory/
RewriteRule (.*) https://my-new-domain.com/my-new-directory/$1 [R=301,L]

or to redirect to the root directory always

RewriteEngine on
RewriteBase /mydirectory/
RewriteRule (.*) http://www.newdomain.com/mydirectory/ [R=301,L]
Not sure the above is correct, the following worked redirecting a subdirectory from a domain to a new domain

Redirect anything in this directory or any sub directory off it: http://myoldomain.com/api/v100/

To this new domain and new sub directory: http://mynewdomain.com/mynewsubdirectory/api/v100/

This in the root “http://myoldomain.com/”.htaccess file worked:

RewriteEngine on
RewriteBase /api/v100/
RewriteRule (.*) http://mydomain.com/mynewsubdirectory/$1 [R=301,L]

But placing it in the .htaccess file in the directory “http://myoldomain.com/api/v100/” instead, this was needed:

RewriteEngine on
RewriteBase /api/v100/
RewriteRule (.*) http://mydomain.com/mynewsubdirectory/api/v100/$1 [R=301,L]
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *