如何使用.htaccess或使用wp_rewrite()更改/翻译查询字符串?
第一个例子
当前网址为:
www.website.com/post-name/ ?edit = post-id
我希望它是:
www.website.com/post-name/的修改 /后-ID
第二个例子
当前网址为:
www.website.com/post-name ?updated = true
我希望它是:
www.website.com/post-name / updated
第三个例子 - WP"默认" ancors
当前网址为:
www.website.com/blog-post-name/ #more - post-id
我希望它是:
www.website.com/blog-post-name /
我真的看过50个关于这个主题的帖子,教程和文章。我无法让它发挥作用。
我尝试过.htaccess的一个基本例子:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^edit/([^/.]+)/?$ ?edit=$1 [L]
</ifModule>
有人可以根据我的一个或多个示例给我写一个例子,我可以用它来解决问题吗?
关于第3个示例 - 我无法从我的任何模板中找到此锚点,有没有办法在不找到它的情况下更改/翻译它并进行硬编码?
答案 0 :(得分:2)
以下似乎适用于第一种和第二种情况
RewriteEngine on
RewriteCond %{QUERY_STRING} edit=(.*)
RewriteRule (.*)/ $1/edit/%1? [NE,L]
RewriteCond %{QUERY_STRING} updated=true
RewriteRule (.*)/ $1/updated? [NE,L]
正如我在上面的评论中所述,#anchors永远不会发送到服务器。
我的解决方案也在this sandbox进行了测试,所以请花些时间来验证它们是否真的适合你。
参考:apache wiki
更新: 我在一个真正的盒子上测试,它有点工作。
我的测试输入是http://www.myawesomesite.com/awesome-post-001/?edit=awe001(顺便说一句:我不拥有该域名,我编辑了/ etc / hosts来伪造它)
这是我的conf
RewriteEngine on
RewriteCond %{QUERY_STRING} edit=(.*)
RewriteRule (.*)/ $1/edit/%1/? [NE,L]
RewriteCond %{QUERY_STRING} updated=true
RewriteRule (.*)/ $1/updated/? [NE,L]
LogLevel alert rewrite:trace6
以下是我的mod_rewrite跟踪日志的摘录。
strip per-dir prefix: /var/www/html/awesome-post-001/ -> awesome-post-001/
applying pattern '(.*)/' to uri 'awesome-post-001/'
RewriteCond: input='edit=awe001' pattern='edit=(.*)' => matched
rewrite 'awesome-post-001/' -> 'awesome-post-001/edit/awe001/?'
split uri=awesome-post-001/edit/awe001/? -> uri=awesome-post-001/edit/awe001/, args=<none>
add per-dir prefix: awesome-post-001/edit/awe001/ -> /var/www/html/awesome-post-001/edit/awe001/
strip document_root prefix: /var/www/html/awesome-post-001/edit/awe001/ -> /awesome-post-001/edit/awe001/
internal redirect with /awesome-post-001/edit/awe001/ [INTERNAL REDIRECT]
strip per-dir prefix: /var/www/html/awesome-post-001/index.html -> awesome-post-001/index.html
applying pattern '(.*)/' to uri 'awesome-post-001/index.html'
RewriteCond: input='' pattern='edit=(.*)' => not-matched
strip per-dir prefix: /var/www/html/awesome-post-001/index.html -> awesome-post-001/index.html
[applying pattern '(.*)/' to uri 'awesome-post-001/index.html'
RewriteCond: input='' pattern='updated=true' => not-matched
pass through /var/www/html/awesome-post-001/index.html
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/ -> awesome-post-001/edit/awe001/
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/'
RewriteCond: input='' pattern='edit=(.*)' => not-matched
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/ -> awesome-post-001/edit/awe001/
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/'
RewriteCond: input='' pattern='updated=true' => not-matched
pass through /var/www/html/awesome-post-001/edit/awe001/
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/index.html -> awesome-post-001/edit/awe001/index.html
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/index.html'
RewriteCond: input='' pattern='edit=(.*)' => not-matched
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/index.html -> awesome-post-001/edit/awe001/index.html
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/index.html'
RewriteCond: input='' pattern='updated=true' => not-matched
pass through /var/www/html/awesome-post-001/edit/awe001/index.html
顺便说一下,你确定使用mod_alias无法解决问题吗?有时解码哪一个最好是非平凡的,只是问。