档案查询提交

时间:2013-07-23 06:47:48

标签: php phpmyid

我有以下代码:

<?php

$id=$_GET['id'];$id=str_replace('_',' ',$id);

global $wpdb;
$res = $wpdb->get_results("SELECT * FROM `agents` WHERE `name`='".$id."'");
foreach ($res as $row) {
  $name = $row->name;
  echo $name;
}
?>

当我点击提交时,个人资料会显示以下网址:http://www.website.com/profile/?id=first_last

有没有办法像这样显示网址:http://www.website.com/profile/first-last/

谢谢,

约翰

1 个答案:

答案 0 :(得分:0)

您可以使用htaccess重写

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^profile/(.*) /profile/?id=$1 [L] 

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

或Wordpress page_rewrite_rules:

您可以将此代码块添加到主题的functions.php文件中。

add_filter( 'page_rewrite_rules', 'my_page_rewrite_rules' );
function my_page_rewrite_rules( $rewrite_rules )
{
    // The most generic page rewrite rule is at end of the array
    // We place our rule one before that
    end( $rewrite_rules );
    $last_pattern = key( $rewrite_rules );
    $last_replacement = array_pop( $rewrite_rules );
    $rewrite_rules +=  array(
        'profile/([0-9]+)/?$' => 'index.php?pagename=profile&id=$matches[1]',
        $last_pattern => $last_replacement,
    );
    return $rewrite_rules;
}

Wordpress Codex:http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

文档:http://www.hongkiat.com/blog/wordpress-url-rewrite/