1

次のURLからURLを書き換える必要があります。

www.example.com/John.Connor/my-first-post/42

に:

www.example.com/post.php?postid=42&userid=19

テーブル:

USER:
id, name, surname, email, password

POST:
id, title, post, userid

それを行う方法はありますか?

4

1 に答える 1

1

mod_rewriteはデータベースにクエリを実行できないため、.htaccess自体を介して完全に実行することはできません。あなたができることは、.htaccessにこのルールを持たせることです:

mod_rewriteと.htaccessを有効にしてからhttpd.conf、次のコードを.htaccessアンダーDOCUMENT_ROOTディレクトリに配置します。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^[^/]+/[^/]+/([0-9]+)/?$ post.php?postid=42 [L,QSA]

次に、post.phpスクリプト内に次のようなコードがあります。

$postid = mysql_real_escape_string($_GET['postid']); 
$userid = mysql_real_escape_string($_GET['userid']);

if (empty($userid)) {
   $userid = mysql_result(mysql_query("SELECT userid FROM POST WHERE id=$posid"), 
              0, "userid");
   // now redirect by appending &userid=49 in the current URL
   header("Location: " . $_SERVER["REQUEST_URI"] . "&userid=" . $userid);
   exit;
}
于 2012-06-12T12:26:27.610 に答える