How to use mod_rewrite to redirect your entire website

Web pages move. That’s a fact of Web development. And if you’re smart, you use 301 redirects to prevent link rot. But what happens if you move the entire website? You could go through and manually write a redirect for each file on the site. But that could take a long time. Fortunately it is possible to use htaccess and mod_rewrite to redirect an entire website with just a few lines of code.

How to use mod_rewrite to redirect your site

  1. At the root of your old web server, edit or create a new .htaccess file using a text editor.
  2. Add the line: RewriteMotor ON
  3. Ad:
     Rewrite rule ^(.*)$$1[R=301,L]

This line will take all the files requested on your old domain and add them (with the same file name) to the URL of your new domain. For example, will be redirected to The R=301 tells the server that the redirect is permanent.

That solution is perfect if you’ve taken your entire site and moved it, intact, to a new domain. But that doesn’t happen very often. A more common scenario is that your new domain has new files and directories. But you don’t want to lose customers who remember the old domain and files. So you need to set your mod_rewrite to redirect all old files to the new domain:

As with the previous rule, the R=301 makes this a 301 redirect. And the L tells the server that this is the last rule.

Once you’ve set your rewrite rule in the htaccess file, your new website will get all page views from the old URL.