简体   繁体   中英

Doing math on Apache RedirectMatch for WordPress

This is similar to this , but I am not using the Perl module.

Problem

I am using WordPress 4.3 and I need to add a number like 1122000 to post_id for permalinks structure.

This is what I have as default URL structure:

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.

My efforts

In my first try I used add_rewrite_rule function:

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 .

In my second try I directly used Apache RedirectMatch like this:

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

in .htaccess file with no success.

Is there another way to implement a 7 digit serial number based on post_id?

Regex does not allow you to do math. RewriteRule doesn't allow you do math either.

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.

Another option would be to write a routing plugin for wordpress, which would allow you to enforce any rewrite rules you liked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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