Wordpress 自定义查询字符串 &漂亮的网址 - 怎么样? [英] Wordpress custom Querystrings & Pretty URL's - How?

查看:29
本文介绍了Wordpress 自定义查询字符串 &漂亮的网址 - 怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完美的(到目前为止)wordpress 设置,用于一个新网站.漂亮的网址按预期工作.

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

/dynamic/?loc=england&code=uk

我也希望使这个 URL 变得漂亮",但每次我修改 .htaccess 时都不会进行任何更改.尝试了一切,并在谷歌上搜索了其他一切——最后的手段.如果我能让以下内容发挥作用,那么我会很高兴.

我需要让 URL 看起来像.

/dynamic/location/england/code/uk/

将此添加到任何 .htaccess 会破坏整个网站.

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

我错过了什么.

提前致谢

N

解决方案

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

WordPress 实际上有一个有点先进的重写引擎,它是标准的——而且它和其他的一样是可插拔的平台.

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

对于类似的示例,请查看我在 WordPress 答案.对此进行扩展,您需要设置类似于以下内容的代码(注意:未经测试!!!):

add_rule('location/([^/]+)/code/([^/]+)','index.php?loc=$matches[1]&code=$matches[2]','最佳');$wp_rewrite->flush_rules(false);//这真的应该在插件激活中完成}

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

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

解决方案

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.

这篇关于Wordpress 自定义查询字符串 &amp;漂亮的网址 - 怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆