I need to (permanent) redirect all of the following:

  1. www.example.com
  2. www.example.com/folder
  3. sub.example.com
  4. sub.example.com/folder

Basically, all URLs that are reachable without being intended to: www.new-example.com, I want to do this within Apache and using the mod_rewrite module via the .htaccess file.


Put this in your .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com[NC]
RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,L]


Note that this .htaccess should, of course, apply to the old domain. Usually in situations like this, the old domain and the new domain point to the same server though, and so get the same .htaccess.

| improve this answer | |

Use a 301 redirect in your .htaccess file at the old domain:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.new-example.com/$1 [R=301,L]
| improve this answer | |

It would be better to redirect (forward) your domain at your registrar and forward the old domain which includes all sub domains to the new domain. This way your server is not handling all the requests which takes up resources.

| improve this answer | |

To redirect all files on your domain use this in your .htaccess file if you are on a UNIX web server:

RedirectMatch 301 ^/(.*)$ http://www.new-example.com/$1
| improve this answer | |
  • RedirectMatch is a mod_alias directive, not mod_rewrite. (If you were going to use mod_alias then the simpler Redirect directive would be preferable in this situation, for example: Redirect 301 / http://www.new-example.com/) – MrWhite Apr 14 '16 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.