使用 .htaccess 使 URL 成为一件美丽的事情

Making URL a beautiful thing to look at using .htaccess

由于 Wordpress %postname% 永久链接,我的大部分 URL 都很好。

我仍然想在编辑 post 和 ?updated=true 时更改一些查询字符串,例如 ?edit=如果您提交编辑。

任何人都可以给我一个简单的例子和​​一些我可以用来提高自己的评论吗?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

// I have no idea what comes here

</IfModule>

1) www.mywebsite.com/post-template/?edit=3242 <-- 号码是 post ID

如何在 / 之后仅显示 post ID 或者将 ?edit= 替换为单词 "potatoe" 例如?

2) www.mywebsite.com/post-template/?updated=true <-- 我不知道如何更改它,没有 ID 或任何东西

如何用单词 "carrot" 替换 ?updated=true?

我通常会抓狂并开始测试,但我了解到 .htaccess 中的错误可能会导致一些严重的问题。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^edit/(.*)$ theprocess.php?edit= [L]
RewriteRule ^edit/(.*)/submitit$ theprocess.php?edit=&updated=true [L]
</IfModule>

以上是供您开始使用的示例(假设您正在使用的 php 文件名为 theprocess.php)。

您将需要研究正则表达式,因为您将在 RewriteRules 中大量使用它们。重要的是 </code> 将被替换为 <code>() 中的实际值,在这种情况下是 .* 意味着 "match anything" 在这一点上。所以在示例中,如果输入 http://example.com/edit/123 那么正在处理的是 theprocess.php?edit=123