mysql - Custom redirects with using SQL

admin2025-01-07  3

We have pages with urls like this:

/?page_id=3&user_id=5

/?page_id=6&user_id=9

etc.

page_id and user_id located in table MySQL Database. And this table has the below structure:

ID, page_id and user_id

If I know ID I can make query to database and get page_id and user_id.

Question: how to make custom redirect? That user can use ID in short url like this

/[ID]/

with redirect to

/?page_id=[page_id]&user_id=[user_id]

Thank you!

We have pages with urls like this:

http://example.com/?page_id=3&user_id=5

http://example.com/?page_id=6&user_id=9

etc.

page_id and user_id located in table MySQL Database. And this table has the below structure:

ID, page_id and user_id

If I know ID I can make query to database and get page_id and user_id.

Question: how to make custom redirect? That user can use ID in short url like this

http://example.com/page/[ID]/

with redirect to

http://example.com/?page_id=[page_id]&user_id=[user_id]

Thank you!

Share Improve this question asked Mar 4, 2015 at 15:28 CityCity 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I wrote function for short url. Maybe it will be helpful for someone:

function short_url(){
    global $post;
    if (preg_match('/^http:\/\/example.com\/page\/[0-9]{1,4}$/', "http://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]))
    { 
        $my_id = intval(preg_replace('/[^0-9]+/', '', $_SERVER["REQUEST_URI"]), 10);
        global $wpdb;
        $myquery = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_mytable WHERE ID=%d", $my_id));
        $short_url="http://example.com/?page_id=".$myquery->page_id."&user_id=".$myqery->user_id;

        wp_redirect($short_url);
        exit();
    }
}
add_action( 'template_redirect', 'short_url' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263108a865.html

最新回复(0)