IT学习者 | 文章大全 | 技术文档 | 桌面壁纸 | 实用查询 | 网络电台 | 成语 | 歇后语 | 网址 | 下载 | 周公解梦 | 生日密码 | 电视剧365 | Flash
 您现在的位置: IT学习者 >> 文章大全 >> 网站运营 >> SEO E文资料

301 Permanent Redirect by Brian V. Bonini

【 作者:Brian V. Bonini    来源:gfx-design  更新时间:2007-9-3 | 字体:

New domain name?

Need to change a file name?

Hierarchy of your servers directory structure change?


meta http-equiv="refresh"... is highly frowned on by search engines and is commonly used by spammers. As such, THIS SHOULD BE AVOIDED.

A 301 Redirect as it is commonly referred to will allow you to make these changes without compromising your hard earned SEO results.

Using the Apache web server, fortunately this is a simply task.

There are a few different places you can set various 'Redirect' directives such as your servers main configuration file (typically httpd.conf) or within a 'Virtual Host' container inside one of your server configuration files. The final method and the one we will be discussing here is using your servers directory Auth file (AKA: .htaccess).

The first thing you do is create a file named .htaccess

There are a couple different way to approach this but in it most simple form you can just issue a 'Redirect' request in your .htaccess file like so:

Redirect /foo http://foobar.com/foo

In this example if the client requests
http://myserver.com/foo/foobar.txt,

it will be told to access
http://foobar.com/foo/foobar.txt instead.

Now this is not quite complete yet, since we need to specifically send a 301 status code. Without the status argument "Redirect" will send a temporary redirect status (302). So we simply take the above example and change it to:

Redirect 301 /foo http://foobar.com/foo

OR

Redirect permanent /foo http://foobar.com/foo

And there is also a specific 'RedirectPermanent' directive as well so you could just as easily say:

RedirectPermanent /foo http://foobar.com/foo

So far so good? Excellent. So now you ask what if I want to change ALL my .htm files to .html. Introducing 'RedirectMatch' which makes use of standard regular expression syntax.

RedirectMatch (.*)\.htm$ http://myserver.com$1.html

Going into an in depth explanation of regular expression if beyond the scope of this tutorial but a quick Google search for "Perl compatible regular expression syntax" should set you on your way.

What the above example does is match any character '.' any number of times '*', i.e., that is it will match everything. (.*) the parenthesis group the result into a variable which we will use with a back-reference ($1) later in the directive. Next, '\' simply escapes the following '.'. Since '.' has special meaning in regular expression syntax we need to escape it if we want to use it in the literal sense, '\' provides the escape sequence. Next is the '.htm' which we know is a static part of the search string we're looking for followed by '$' which simply marks the end of the pattern in a regular expression (regex). So the above example takes any file with an .htm extension and redirects the client to the same file at the location specified, i.e., http://myserver.com$1.html

But wait, what's that '$1'? Well remember we said we were going to store a variable for use in a back reference later, well here it is. The '$1' simply says that whatever was in (.*) is now represented by $1

So, the client request for example foobar.htm, the above directive matches this and redirects the client to http://myserver.com/foobar.html because 'foobar' is stored in '$1'. Just as with 'Redirect', 'RedirectMatch' takes a status argument so to issue a 301 (permanent) redirect we do this:

RedirectMatch 301 (.*)\.htm$ http://myserver.com$1.html

Another method we can use is via mod_rewrite. This requires that the mod_rewrite module is active on your webserver. It usually is and is done by the system administrators when they installed the webserver. mod_rewrite is a very powerful URL re-writing engine and we will only by scratching a hair on its head here.

Again, in your .htaccess file

RewriteEngine ON
RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301,L]

The above example will re-map your old domain to a new one and issue a 301 status code (permanent redirect). So a request for

http://olddomain.com/foobar.html will go to

http://mynewdomain.com/foobar.html

If you simply want to redirect all requests regardless of the page requested to the new domain you could use:

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

In this case no matter what file or directory is requested they will all go to

http://mynewdomain.com/ i.e., http://myolddomain.com/foobar.html

will go to http://mynewdomain.com/

The [R=301,L] means redirect the client and send a 301 status code (R=301) and make this the last rule (L).

Whichever method you decide on, once completed upload the resulting file to your webservers document root and you should be on your way.


Related 301 Redirect and Resources

Also, a Google search for '301 redirect+seo' or 'mod_rewrite+301 redirect' should turn up tons of information.

相 关 文 章
没有相关文章
相 关 软 件
没有相关下载
逃生 放生 黄玫瑰 想太多 那滋味 擦肩而过 放手去爱 北京欢迎你 依然在一起 吻得太逼真 感动天感动地 坐上火车去拉萨 怎么会狠心伤害我
心碎 冲动 小太阳 别碰我 蒲公英 千山万水 改变自己 一定要爱你 等爱的玫瑰 陷入爱里面 北极星的眼泪 最后一次的温柔 亲爱的那不是爱情
光荣 火花 坏女人 日不落 樱花草 为你写诗 独家记忆 夏天的味道 寂寞才说爱 忘不掉的伤 爱上你是个错 第三者的第三者 地球人都知道我爱你
假如 相思 是非题 有缘人 舍不得 我的答铃 死而无憾 外滩十八号 越爱越难过 123木头人 和寂寞说分手 爱上你是我的错 爱情里没有谁对谁错
加入收藏留言建议自助友情链接普通友情链接站长的Blog
版权所有   COPYRIGHT 2002-2008 ★IT学习者★ ALL RIGHTS RESERVED.