简体   繁体   English

在 Apache RedirectMatch for WordPress 上做数学运算

[英]Doing math on Apache RedirectMatch for WordPress

This is similar to this , but I am not using the Perl module.这类似于,但我不使用Perl模块。

Problem问题

I am using WordPress 4.3 and I need to add a number like 1122000 to post_id for permalinks structure.我正在使用 WordPress 4.3,我需要向post_id添加一个像1122000这样的1122000以获取永久链接结构。

This is what I have as default URL structure:这是我的默认 URL 结构:

mysite.com/?post_type=orders&p=139

and this is desired permalink structure:这是所需的永久链接结构:

mysite.com/orders/1122139

Please note that we can't just concatenate 1122 into the post_id as post_id may exceed 999 at future.请注意,我们不能将1122连接到post_id因为post_id将来可能会超过999

My efforts我的努力

In my first try I used add_rewrite_rule function:在我的第一次尝试中,我使用了add_rewrite_rule函数:

function my_rewrite_basic() {
    add_rewrite_rule( '^orders/1122([0-9]+)/?', 'index.php?post_type=orders&p=$matches[1]', 'top');
}
add_action('init', 'my_rewrite_basic');

Which works only if id is lower than 1000 .仅当 id 低于1000时才有效。

In my second try I directly used Apache RedirectMatch like this:在我的第二次尝试中,我直接使用了 Apache RedirectMatch,如下所示:

RedirectMatch 301 ^/orders/(\d+) /?post_type=orders&p=($1-1122000)

in .htaccess file with no success..htaccess文件中没有成功。

Is there another way to implement a 7 digit serial number based on post_id?有没有另一种方法可以实现基于 post_id 的 7 位序列号?

Regex does not allow you to do math.正则表达式不允许你做数学。 RewriteRule doesn't allow you do math either. RewriteRule也不允许你做数学。

You should look into modifying the data in the database.您应该考虑修改数据库中的数据。 You could add that number to the post id in the database, while taking care that no foreign key constrainst are broken (That means, either you configure explicit foreign keys with on update cascade option, or you update manually every field that contains a reference to the post id.您可以将该数字添加到数据库中的帖子 ID,同时注意不要破坏外键约束(这意味着,您可以使用on update cascade选项配置显式外键,或者手动更新包含对帖子 ID。

Another option would be to write a routing plugin for wordpress, which would allow you to enforce any rewrite rules you liked.另一种选择是为 wordpress 编写一个路由插件,这将允许您强制执行您喜欢的任何重写规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM