Translate/change URL 中的查询字符串

Translate/change query strings in URL

如何 change/translate 使用 .htaccess 或使用 wp_rewrite() 查询字符串?

第一个例子

当前 URL 是:

www.website.com/post-name/?edit= post-id

我希望它是:

www.website.com/post-name/编辑/post-id

第二个例子

当前 URL 是:

www.website.com/post-name ?updated=true

我希望它是:

www.website.com/post-name /更新

第三个例子-WP "default" ancors

当前 URL 是:

www.website.com/blog-post-name/ #more- post-id

我希望它是:

www.website.com/blog-post-name/


我确实看了 50 post 篇关于这个主题的教程和文章,但我无法让它发挥作用。

我尝试使用 .htaccess 的一个基本示例:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^edit/([^/.]+)/?$ ?edit= [L]
</ifModule>

有人可以根据我的一个或多个例子写一个例子给我吗?

关于第 3 个示例 - 我无法从我的任何模板中找到这个锚点,有没有办法 change/translate 它而不用找到它并对其进行硬编码?

以下似乎适用于第一种和第二种情况

RewriteEngine on

RewriteCond %{QUERY_STRING} edit=(.*)
RewriteRule (.*)/ /edit/%1? [NE,L]

RewriteCond %{QUERY_STRING} updated=true
RewriteRule (.*)/ /updated? [NE,L]

正如我在上面的评论中所述,# 锚点永远不会发送到服务器。

我的解决方案也在 this sandbox 中进行了粗略测试,所以请花点时间验证它们是否真的适合你。

参考:apache wiki

更新: 我在一个真实的盒子上测试过,它有点用。

我的测试输入是 http://www.myawesomesite.com/awesome-post-001/?edit=awe001(顺便说一句:我不拥有那个域,我编辑 /etc/hosts 来伪造它)

这是我的配置文件

    RewriteEngine on

    RewriteCond %{QUERY_STRING} edit=(.*)
    RewriteRule (.*)/ /edit/%1/? [NE,L]

    RewriteCond %{QUERY_STRING} updated=true
    RewriteRule (.*)/ /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 解决您的问题吗?有时解码哪个最好是非常重要的,只是问问。