WordPress 自定义查询字符串和漂亮的 URL - 如何实现?

发布于 2024-10-10 07:37:38 字数 499 浏览 0 评论 0原文

我有一个非常好的(到目前为止)新网站的 WordPress 设置。漂亮的网址按预期工作。

我有 1 个动态页面,根据查询字符串加载内容:

/dynamic/?loc=england&code=uk 

我也希望使此 URL 变得“漂亮”,但每次修改 .htaccess 时都不会进行任何更改。尝试了所有方法并用谷歌搜索了其他所有方法 - 最后的手段。如果我能让以下工作发挥作用,我会很高兴。

我需要使 URL 看起来像。

/dynamic/location/england/code/uk/

将其添加到任何 .htaccess 都会破坏整个网站。

RewriteRule /dynamic/(.*)/(.*)/(.*)/(.*)/$ /dynamic?$1=$2&$3=$4

我缺少什么。

预先感谢

N

I have a perfectly good (so far) setup of wordpress of for a new website. The pretty urls are working as expected.

I have 1 dynamic page that loads content depending on the querystring:

/dynamic/?loc=england&code=uk 

I wish to make this URL "pretty" as well but every-time I modify the .htaccess no changes are made. Tried everything and googled everything else - last resort. If I could get the following to work then I would be happy.

I need to make the URL look like.

/dynamic/location/england/code/uk/

Adding this to any .htaccess breaks the whole website.

RewriteRule /dynamic/(.*)/(.*)/(.*)/(.*)/$ /dynamic?$1=$2&$3=$4

What am i missing.

Thanks in advance

N

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-10-17 07:37:38

不要将重写规则添加到您的 .htaccess 文件中。 WordPress 会为您管理该文件,因此请尽可能尝试使用内置功能。

WordPress 实际上有一个稍微先进的重写引擎,它是标准的 - 而且它就像平台的其他部分一样是可插拔的。

不过,诀窍是利用它。您需要注册您的正则表达式,以便 WordPress 知道要匹配什么类型的字符串(即 dynamic/location/(.*)/code/(.*) => /dynamic? $loc=$1&code=$2)。然后,您需要在后端设置页面和脚本来处理提交。

对于类似的示例,请查看我收到的关于使用自定义帖子类型解析自定义 URL 的答案 WordPress 答案。扩展它,您需要设置类似于以下的代码(注意:未经测试!!!):

<?php
add_action('init', 'add_my_rewrite');

function add_my_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_rule('location/([^/]+)/code/([^/]+)','index.php?loc=$matches[1]&code=$matches[2]','top');
    $wp_rewrite->flush_rules(false);  // This should really be done in a plugin activation
}

应该将重写结构添加到您的中。 htaccess 自动生成文件。

Don't add the rewrite rule to your .htaccess file. WordPress manages that file for you, so try to use built-in features whenever you can.

WordPress actually has a somewhat advanced rewrite engine that ships standard - and it's pluggable just like the rest of the platform.

The trick, though, is working with it. You'll need to register your RegEx so WordPress knows what kinds of strings to match (i.e dynamic/location/(.*)/code/(.*) => /dynamic?$loc=$1&code=$2). Then you'll need to set up the page and script on the back end to handle the submission.

For a similar example, look at the answer I received to parsing custom URLs with a custom post type over on WordPress Answers. Extending this, you'll need to set up code similar to the following (note: untested!!!):

<?php
add_action('init', 'add_my_rewrite');

function add_my_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_rule('location/([^/]+)/code/([^/]+)','index.php?loc=$matches[1]&code=$matches[2]','top');
    $wp_rewrite->flush_rules(false);  // This should really be done in a plugin activation
}

This should add the rewrite structure to your .htaccess file automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文