Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Apache .htaccess是否为父级而非子级重定向?_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Apache .htaccess是否为父级而非子级重定向?

Apache .htaccess是否为父级而非子级重定向?,apache,.htaccess,mod-rewrite,redirect,Apache,.htaccess,Mod Rewrite,Redirect,我有许多URL需要重定向到新的位置,但在某些情况下,子页面需要保持活动状态而不重定向。例如: /products将重定向到http://www.newsite.com/products /products/category1将重定向到http://www.newsite/products/category1 但是/products/specialitem根本不会被重定向 使用重定向或重定向匹配是否可能 执行重定向301/产品http://www.newsite.com/products似乎会影响

我有许多URL需要重定向到新的位置,但在某些情况下,子页面需要保持活动状态而不重定向。例如:

/products
将重定向到
http://www.newsite.com/products

/products/category1
将重定向到
http://www.newsite/products/category1

但是
/products/specialitem
根本不会被重定向

使用重定向或重定向匹配是否可能

执行
重定向301/产品http://www.newsite.com/products
似乎会影响所有子页面

谢谢你的指导

编辑:

使用waynethec的答案,我可以开始了。但有谁能澄清为什么我下面的第一条规则有效,而其他规则无效

重定向匹配301^第一段$http://www.google.com/

RedirectMatch 301^第一段/第二段$http://news.google.com/

RedirectMatch 301^第一段/第二段/第三段$http://cnn.com/

RedirectMatch 301^第一段/第二段/第三段/foobar$http://gbv.com/


(不工作意味着我仍然可以访问页面,而不是重定向页面。)

您应该能够使用以下重定向匹配规则:

RedirectMatch 301 ^/products$ http://www.newsite.com/products

请注意,这将只重定向对/products的请求,而不是/products/或/products/pagename.extension。

您可以使用
RedirectMatch

RedirectMatch 301 ^/products(?!/specialitem)(.*)$ http://www.newsite.com/products$1

这将重定向
/products
,或它之后的任何内容,除了
/products/specialitem/
如果您需要添加条件,这也适用于我。注意,在使用RewriteRule时,我必须消除
^
产品之间的斜杠

RewriteCond [...whatever your conditions are...]

# test with 302 first to avoid annoying caching issues
# RewriteRule ^products$  http://www.newsite.com/products [R=302,NC,L]

# but use 301 in production once you know it's working
RewriteRule ^products$  http://www.newsite.com/products [R=301,NC,L]

欢迎来到
mod_rewrite
)的世界当然,你可以使用RedirectMatch来实现这一点,但是如果需求数量增加,请切换到RewriteRules<代码>重定向匹配301^/产品(?!/specialitem)http://www.newsite.com/products
但没有检查。这确实很有帮助,但如果我的URL中有更多的段会怎么样?例如:
/category1/subcat1
重定向到
http://newsite.com/products
/category1/subcat1/product1/details/
重定向到
http://newsite.com/products/somepage
/category1/subcat1/product2/details2/
重定向到
http://newsite.com/products/somepage2
/category1/subcat2/foo/bar/
重定向到
http://newsite.com/products/lorem/ipsum
也许这会更好地问我的后续问题:为什么下面的第一条规则对我有效,而后面的规则对我无效<代码>重定向匹配301^第一段$http://www.google.com/
重定向匹配301^第一段/第二段$http://news.google.com/
重定向匹配301^第一段/第二段/第三段$http://cnn.com/
重定向匹配301^第一段/第二段/第三段/foobar$http://gbv.com/
您所使用的语法提供的是有效的,应该按照您的期望工作。在.htaccess中是否有其他规则?