mod_rewrite 相当于 RedirectMatch 规则
我的 .htaccess 文件中有一个 RedirectMatch 规则,工作正常,但我无法使用 mod_rewrite 提出类似的规则。
我的目标是让此 URL:mysite.com/anything/print 显示此页面:mysite.com/anything?view=print。
适用于重定向的规则是这样的:
RedirectMatch 301 ^(.*)/print/?$ http://mysite.com/$1?view=print
但现在我想使用 mod_rewrite 将其从可见的 301 重定向更改为“不可见”重写。我对此尝试了许多不同的变体(有或没有 RewriteBase),但没有一个有效:
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/ 附加到永久链接时,WP_Debug 插件会指示以下内容:
请求: myposttype/mypost/print
查询字符串: attachment=print
匹配重写规则: myposttype/[^/]+/([^/]+)/?$
匹配重写查询: 附件=打印
I have a RedirectMatch rule in my .htaccess file that works fine, but I'm having trouble coming up with a comparable rule using mod_rewrite.
My goal is to have this URL: mysite.com/anything/print show this page: mysite.com/anything?view=print.
The rule that works to Redirect it is this:
RedirectMatch 301 ^(.*)/print/?$ http://mysite.com/$1?view=print
But now I'd like to change this from a visible 301 redirect to an "invisible" rewrite using mod_rewrite. I have tried many different variations on this (with and without RewriteBase), and none have worked:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/print/? $1?view=print
What am I doing wrong? Mod_rewrite is definitely enabled, and there are functioning Wordpress-based mod_rewrite rules in the same .htaccess file.
UPDATE
Using tips from @Nathan, I now have this. However, I still get a 404 when I visit mypost/print.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [L]
When I append /print/ to a permalink, the WP_Debug plugin indicates the following:
Request: myposttype/mypost/print
Query String: attachment=print
Matched Rewrite Rule: myposttype/[^/]+/([^/]+)/?$
Matched Rewrite Query: attachment=print
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 htaccess 文件中有典型的 WordPress 规则,则您的规则应位于
# BEGIN WordPress
块之前。否则,wordpress 规则将在调用您的规则之前停止重写匹配。另外,您应该在正则表达式模式后添加
$
,除非您还想匹配以下内容:http://domain.com/page/print/something/else/here
最后,为了让 WordPress 识别 URL 的更改而不重定向页面,您需要将 URL 附加到
index.php/
,以便 Wordpress 使用路径信息永久链接。例如
If you have typical wordpress rules in the htaccess file, your rules should come before the
# BEGIN WordPress
block. Otherwise the wordpress rules will stop the rewrite matching before your rules get called.Also, you should add
$
after your regex pattern unless you also want to match something like:http://domain.com/page/print/something/else/here
Lastly, in order for Wordpress to recognize the change in the URL without redirecting the page, you need to append the URL to
index.php/
so that Wordpress will use path info permalinks.E.g.
或者
URL 字符串末尾不带“/”
or
without "/" in the end of URL string