Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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

share|improve this question

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.newdomain.com/$1
share|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.newdomain.com/) – w3dk Apr 14 at 7:25

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.

share|improve this answer

Put this in your .htaccess:

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

Source

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.

share|improve this answer

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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