Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
301.htaccess奇怪的行为和精确匹配_.htaccess_Redirect - Fatal编程技术网

301.htaccess奇怪的行为和精确匹配

301.htaccess奇怪的行为和精确匹配,.htaccess,redirect,.htaccess,Redirect,我最近移动了我的博客,我只是301重定向了几页。问题是我发现了一个恼人的问题 有两个链接: hxxp://www.domain.com/category/post.html hxxp://www.domain.com/category/ 我想把这两个都转到一个新的域,所以写了以下内容 Redirect 301 hxxp://www.domain.com/category/post.html hxxp://new.domain.com/category/post.html 那很好,然后我做了

我最近移动了我的博客,我只是301重定向了几页。问题是我发现了一个恼人的问题

有两个链接:

hxxp://www.domain.com/category/post.html
hxxp://www.domain.com/category/
我想把这两个都转到一个新的域,所以写了以下内容

Redirect 301 hxxp://www.domain.com/category/post.html  hxxp://new.domain.com/category/post.html
那很好,然后我做了分类

Redirect 301 hxxp://www.domain.com/category/ hxxp://new.domain.com/
由于类别结构是不同的,我只是想301它的主页

问题是,这个重定向正在影响第一个重定向,大概是因为它与第一个重定向非常相似

你知道我怎么才能找到精确的匹配吗


谢谢

AFAIK重定向始终将目录重定向到目录

您可以使用ModRewrite

RewriteRule ^category/$ http://newdomain/ [R=301] 
重定向1页

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

要将所有目录重定向到1(主)页

进行此类匹配,您需要使用mod_rewrite,即URL重写的瑞士军刀;-)


您可以尝试使用RedirectMatch指令(),该指令允许您使用正则表达式匹配要重定向的url。使用此选项,可以在末尾添加$符号以指定字符串的结尾:

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/
这样,重定向只会影响精确匹配

或者,您可以像这样使用RewriteRule指令():

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 ]

L旗(代表最后一个)强制Apache停止处理其他重写指令

您给出规则的顺序是什么。只需删除
hxxp://www.domain.com
部分,你很好:
重定向
指令的旧URL部分应该以斜杠开头,并且不应该包含域名:如果放在
.htaccess中,这将不会工作--URL模式将不包含前导斜杠
/
。但如果放置在配置或虚拟主机上下文中,则可以正常工作。如果放置在
.htaccess
中,这些重写规则将不起作用--URL模式将不包含前导斜杠
/
。但如果放在配置或虚拟主机上下文中,将很好地工作。我不知道这个区别。然后,您可以将前导的
/
替换为
^
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 ]