Apache mod_重写相当于重定向匹配规则

Apache mod_重写相当于重定向匹配规则,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我在.htaccess文件中有一个重定向匹配规则,它工作得很好,但是我在使用mod_rewrite来创建一个类似的规则时遇到了麻烦 我的目标是让这个URL:mysite.com/anything/print显示这个页面:mysite.com/anything?view=print 用于重定向它的规则如下: RedirectMatch 301 ^(.*)/print/?$ http://mysite.com/$1?view=print 但现在我想用mod_rewrite将其从可见301重定向更

我在.htaccess文件中有一个重定向匹配规则,它工作得很好,但是我在使用mod_rewrite来创建一个类似的规则时遇到了麻烦

我的目标是让这个URL:mysite.com/anything/print显示这个页面:mysite.com/anything?view=print

用于重定向它的规则如下:

 RedirectMatch 301 ^(.*)/print/?$ http://mysite.com/$1?view=print
但现在我想用mod_rewrite将其从可见301重定向更改为“不可见”重写。我在这方面尝试了许多不同的变体(有重写基和没有重写基),但都没有成功:

 RewriteEngine On
 RewriteBase /
 RewriteRule ^(.*)/print/? $1?view=print
我做错了什么?Mod_rewrite已明确启用,并且在同一.htaccess文件中有基于Wordpress的Mod_rewrite规则


更新

使用@Nathan的提示,我现在有了这个。然而,当我访问mypost/print时,我仍然得到了404

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [L]
当我将/print/附加到permalink时,WP_调试插件指示以下内容:

请求:
myposttype/mypost/print

RewriteEngine on
RewriteRule ^(.*)/print/$ /$1?view=print [L]
查询字符串:
附件=打印

RewriteEngine on
RewriteRule ^(.*)/print/$ /$1?view=print [L]
匹配的重写规则:
myposttype/[^/]+/([^/]+)/?$

匹配的重写查询:
attachment=print

RewriteEngine on
RewriteRule ^(.*)/print/$ /$1?view=print [L]


如果您在htaccess文件中有典型的wordpress规则,则您的规则应位于
#BEGIN wordpress
块之前。否则,wordpress规则将在调用规则之前停止重写匹配

此外,您应该在regex模式之后添加
$
,除非您还希望匹配类似以下内容:
http://domain.com/page/print/something/else/here

最后,为了让Wordpress在不重定向页面的情况下识别URL中的更改,您需要将URL附加到
index.php/
,以便Wordpress使用路径信息永久链接

例如

重新编写引擎打开
重写基/
重写cond%{REQUEST_URI}^/索引\.php
重写规则^(.*)/print/?$/index.php/$1?视图=打印[L]
#开始WordPress
重新启动发动机
重写基/
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]
#结束WordPress

这似乎应该能用,但当我访问domain.com/mypost/print时,我还是得到了404。使用WP调试插件,我看到查询正在查找attachment=print(一个带有“print”段塞的媒体附件)。你能想出解决这个问题的办法吗?WordPress将使用原始URL解析永久链接,除非你将重定向改为使用路径信息永久链接,如index.php/URL/path/here?view=print。我已经更新了示例以包含路径信息方法。非常感谢您的更新。404和attachment=打印查询与更新的代码相同;冲洗permalinks没有帮助。手动插入index.php(例如mysite.com/index.php/mypost/print)也不起作用,并给出相同的404。有什么想法吗?我已经更新了这个问题,以显示我从你的新代码中得到的WP_调试结果。