Forum Topic: Redirecting a whole site using mod_rewrite

Forum: .htaccess Forum : Redirecting • Posted by Yael Miller • Updated:

I'm deleting a website and redirecting it to another website.

I (now) know I can easily do this with mod_alias but since I plan to keep 5G in the .htaccess I want to use mod_rewrite to make sure it's done first.

In my previous experience redirecting whole sites, I go to my cPanel > Addon Domains > Manage Redirection and tell it where I want the domain URL to redirect to. And it automatically sticks the below in my .htaccess

RewriteCond %{HTTP_HOST} ^oldsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.com$
RewriteRule ^/?$ "http\:\/\/www\.newsite\.com\/" [R=301,L]

And then for all the individual pages on the old site that I wanted to go to specific pages on my new site I would do:

Redirect 301 /pancakes/ http://newsite.com/pancakes/

This time I want to redirect every single page on the site to one page (the new site's home page) on a new site.

If I was using mod_alias, I think I would use the below. (Is this correct?)

RedirectMatch 301 / http://newsite.com/

But how do I redirect -- using mod_rewrite -- every single page on a site to the one page on a new site?

7 Replies to “Redirecting a whole site using mod_rewrite”

Jeff Starr
Posted by Jeff Starr

I think something like this would be the equivalent method using mod_rewrite:

RewriteRule .* http://www.newsite.com/ [R=301,L]

The trick of course is adding this directive to the .htaccess of the old site.

PS - don't use the slashes to escape characters in the destination URL in any RewriteRule.

Posted by Yael Miller •

I did the below and everything is redirecting correctly. Thanks.

RewriteRule .* "http\:\/\/example\.com\/" [R=301,L]

Jeff Starr
Posted by Jeff Starr

Awesome. And just for the sake of anyone else who is reading, the destination URL should not be quoted or escaped in any way, as it's a literal value and not an expression.

Note: I had mistakenly included quotes around the destination URL, http://www.newsite.com/, in my previous example, but have removed them because they could cause problems on the server.

So for anyone who may be confused at this point, here is the correct way of redirecting everything via mod_rewrite:

RewriteRule .* http://example.com/ [R=301,L]

Posted by Yael Miller •

Ok, I just dumped the quotation marks and the escaping and did your recommendation above. It worked.

So why does cPanel when it auto-creates a mod_write, use quoatation marks and escaping?

Jeff Starr
Posted by Jeff Starr

Good question. And I would also ask why cPanel does a number of other weird things. For example, it likes to automatically change existing .htaccess rules when you add rules via cPanel settings.

I have no idea why cPanel decides it's necessary, but it could also be a cPanel/shared-hosting server-configuration thing.

In any case, the Apache documentation shows usage without quotes, so that's why I recommend against using them. They may work now, but if something changes, Apache is updated, or whatever it is, then the rule(s) could break. So it's best to play it safe and by the book, imho.

Posted by Yael Miller •

Do I still need to use escaping for this part?

RewriteCond %{HTTP_HOST} ^oldsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.com$
Jeff Starr
Posted by Jeff Starr

Yes, because those are not literal values, they are expressions. For example, if you do not escape the dots, they will be evaluated as wildcard characters to match any single character. By escaping them, they are treated as literal periods.