开发者

htaccess 301 redirect url

I have a small dilemma.

I have been working on a project for almost a year now and had an o开发者_如何学编程ld domain name. Not knowing how Google has indexed the whole site under the old domain which has worried me. I want to put the site up on my new domain name and I think doing a 301 is the way forward. I will need to do this on every dynamic page. The good thing is the page structure is identical.

Any advice on the best way to do this would be massively appreciated.


The best way is to create a .htaccess file if you don't have one already and stick it in your html folder.

<IfModule mod_rewrite.c>
  RewriteEngine on

  RewriteRule ^old/page new/page [R=301,L] #can copy and paste this as many times as needed

</IFModule>


This will redirect everything from olddomain.com to newdomain.com:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,QSA,L]

You can put that mod_rewrite code into your olddomain's .htaccess.


If your hosting place does not support mod_rewrite (yep, there are such hosting companies) or it does not work for some strange and unknown reason, go with a simpler and widely available directive: Redirect or RedirectMatch.

Put such line into your .htaccess on old domain:

Redirect 301 / http://www.newdomain.com/

The above line will redirect ALL URLss from old domain into new domain suing 301 code (Permanent Redirect) which is the right thing from SEO perspective.

If you need to redirect such a single page or point it to a specific new location, use such rule (the same as above just specifying exact URL):

Redirect 301 /help-shipping.html http://www.newdomain.com/help/shipping.php

Apache mod_rewrite manual has a page: When NOT to use mod_rewrite. This particular scenario is listed there (Redirect is much lighter that RewriteRule in terms of resources, which can be an issue on very busy servers).


There is a way to do it via php (.htaccess is the best), but this has come in handy for me once or twice.

<?php
//this can be set however you need it to be
$dynamic_redirect = "http://foo.com".$your_variable; 
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$dynamic_redirect);
exit();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜