如何使用.htaccess 301重定向和更改URL?

问题描述:

I just discovered that I have some duplicate pages that I need to remove but some pages that should not exist are indexed and generating small amounts of traffic. I want to redirect those urls to the original ones.

http://www.example.com/buy-something.php

to

http://www.example.com/something.php

I basically need to remove the "buy-" in the urls and make sure the page is redirected to the proper page. Here is what I have so far:

#301 Redirect buy- to none
RewriteRule ^([a-zA-Z\.]+).php$  /buy-$1.php  [L,R=301]

But this does nothing to the pages that should be redirected and adds a loop of buy-buy-buy-buy-buy-buy- to other pages and causes them to time out. I have tried a few other variations but to no prevail.

Your help is greatly appreciated.

我刚刚发现我需要删除一些重复的页面,但是一些不存在的页面已编入索引, 产生少量流量。 我想将这些网址重定向到原始网址。 p>

http ://www.example.com/buy-something.php p>

p>

http://www.example.com/something.php p>

我基本上需要删除 在网址中“购买 - ”并确保将网页重定向到正确的网页。 以下是我到目前为止: p>

 #301 Redirect buy-to none 
RewriteRule ^([a-zA-Z \。] +)。php $ / buy-  $ 1.php [L,R = 301] 
  code>  pre> 
 
 

但这对应该重定向的页面没有任何作用,并添加了一个buy-buy-buy-buy循环 - 买 - 买到其他页面并导致它们超时。 我尝试了其他一些变化,但没有取得胜利。 p>

非常感谢您的帮助。 p> div>

You mixed up the syntax, right now you are redirecting any .php to /buy-.php, since you want it the other way arround try:

RedirectRule ^buy-([a-zA-Z\.]+).php$  /$1.php  [L,R=301]

That should take any buy-*.php domains and redirect them to *.php with the Code 301.

Source: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Your rewrite rule is incorrect. It does the opposite to what you intended. Try this:

#301 Redirect buy- to none
RewriteRule ^buy-(.*).php$  /$1.php  [L,R=301]