开发者

301 .htaccess strange behavior and exact matches

I've recently moved my blog and I'm just 301 redirecting a few pages. Trouble is I've found an annoying issue.

There are two links -

hxxp://www.domain.com/category/post.html
hxxp://www.domain.com/category/

I'd like to 301 both to a new domain and so wrote the following...

Redirect 301 hxxp://w开发者_运维知识库ww.domain.com/category/post.html  hxxp://new.domain.com/category/post.html

That worked fine, then I did the category...

Redirect 301 hxxp://www.domain.com/category/ hxxp://new.domain.com/

Since the category structure is different I just wanted to 301 it to the homepage.

Trouble is, this redirect is affecting the first redirect, presumably because it's very similar to the first.

Any ideas how I can only 301 redirect exact matches?

Thanks!


AFAIK Redirect always redirect directory to directory

You can use ModRewrite

RewriteRule ^category/$ http://newdomain/ [R=301] 

to redirect 1 page

RewriteRule ^category/.*$ http://newdomain/ [R=301]

to redirect all directory to 1 (main) page


For this kind of matching you want to use mod_rewrite, the swiss army knife of URL rewriting ;-)

RewriteEngine On
RewriteRule /category/post.html$ http://new.domain/post.html [L,R=301]
RewriteRule /category.* http://new.domain/ [L,R=301]


You could try using the RedirectMatch directive (see docs here) that allows you to use a regular expression to match the url to be redirected. With this, you can add a $ symbol at the end to specify the end of the string:

RedirectMatch 301 hxxp://www.domain.com/category/post.html$ hxxp://new.domain.com/category/post.html
RedirectMatch 301 hxxp://www.domain.com/category/$ hxxp://new.domain.com/

This way, the redirect will only affect exact matches.

Alternatively, you could use the RewriteRule directive (docs here) like this:

RewriteEngine On
RewriteRule /category/post.html$  hxxp://new.domain.com/category/post.html [ R=301, L ]
RewriteRule /category/$  hxxp://new.domain.com/ [ R=301, L ]

The L flag (stands for Last) forces Apache to stop processing other Rewrite directives

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜