次の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
それを行う方法はありますか?
次の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
それを行う方法はありますか?
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;
}