douyou9923 2016-02-02 11:39
浏览 56
已采纳

.htaccess 301动态重定向匹配数据库

I have incoming url http://www.example.com/index.html?id=1234

I have database WORDS with 2 columns: 'id' and 'url'

I need to 301 redirect matching the id parameter in url (1234) to corresponding URL, such as:

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

I tried something similar as what is explained here I think the correct way is having a simple .htaccess code that uses a php file to match ids with urls in database but i don't know where to start.

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • duanli9001 2016-02-02 11:52
    关注
    <?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!";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址