Forum Moderators: phranque

Message Too Old, No Replies

Correct way to do 301 redirect

         

brassneck

7:47 am on Nov 30, 2009 (gmt 0)

10+ Year Member



I created the following, is the correct way to the .htaccess?

RewriteEngine On
RewriteRule ^.*$ http://www.example.com [R=301]

I assume this will redirect any page in the originating site to www.example.com?

If I want to redirect a particular page to a new specific page, then what is the code for that please?

Finally, is there any reason for limiting the number of 301s to a particular site. I have about related 20 sites which have lowish traffic which I want to consolidate traffic from into a single domain.

Thanks
Stephen.

jdMorgan

2:40 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should study-up on both mod_rewrite and on the SEO aspects of this before starting.

For example, your code does not "redirect all pages," it redirects all requests. You are, for example, redirecting requests for "olddomain.com/robots.txt", "olddomain.com/sitemap.xml" and "olddomain.com/favicon.ico" to the index page of your new domain -- Not really a good idea unless you want to confuse search engines and make images look broken, etc.

There's also the SEO aspect: Redirecting a whole bunch of domains to one all at once is not generally recommended -- it's better to do them a few URLs at a time -- dozens, not hundreds or thousands.

The Apache mod_rewrite documentation links to their "URL Rewriting Guide" containing many permutations of basic external redirects and internal rewrites. Our Apache Forum Charter [webmasterworld.com] similarly contains links to useful documentation and tutorial resources. Our Apache Forum Library [webmasterworld.com] also contains several useful example threads.

In light of the fact that a single typo in mod_rewrite can sink your sites, and that only you know all of the details of how your sites are constructed and related, it would really be a good idea to spend some research time, rather than rushing into this and relying on "somebody on some forum" to find the right answer for you.

Also, as we're heading into the Holiday season, I'd add that now is not the time to be implementing massive changes to your sites' URL-structure if they are at all commercial; Updating URLs in search can take several weeks to many months, and your ranking will likely suffer (temporarily) in the interim -- best to wait until just before your "annual slow season" to start this sort of project...

Further, if these sites are all hosted in the same filespace on the same server, then you will need to check %{HTTP_HOST} using a RewriteCond, to make sure that only specific hostname requests are redirected, and to make sure that "www.example.com" is not redirected to itself, causing an "infinite" redirection loop.

Jim