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 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线