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.

4 Answers 4


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.


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]

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.


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
  • 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, 2016 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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