簡體   English   中英

.htaccess 301動態重定向匹配數據庫

[英].htaccess 301 dynamic redirect matching database

我有輸入網址http://www.example.com/index.html?id=1234

我有2列的數據庫WORDS:“ id”和“ url”

我需要301將網址(1234)中的id參數匹配的重定向到相應的網址,例如:

http:// www.example.com/index.html?id=1234變為http:// www.redirectedurl.com

我嘗試過類似此處解釋的內容我認為正確的方法是擁有一個簡單的.htaccess代碼,該代碼使用php文件將id與數據庫中的url進行匹配,但我不知道從哪里開始。

謝謝你的幫助!

<?php

    //Check if GET value is set
    if(isset($_GET["id"]){

        //Retrieve values from database
        $db = new PDO(/* connect to your database here */);
        $stmt = $db->prepare("SELECT `url` FROM `table` WHERE `id`=? LIMIT 1");

        //If the statement doesn't execute
        if(!$stmt->execute(array(trim($_GET["id"])))) exit("Could not connect to database!");

        //If there are no rows returned
        if($stmt->rowCount()<1) exit("Could not find ID!");

        //Data is correct - redirect the user!
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $url = $row["url"];
        header("Location: ".$url);
        exit;            
    } else {
        echo "No ID set!";
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM