dongting3135 2018-06-13 17:35
浏览 51

如何根据URL和SQL数据库中的相关值将访问者重定向到特定页面?

I'm looking for a conceptual starting point here, or even if someone could point me in the right direction.

Given that someone goes to a website with URL "www.website.com/123", how could I programatically grab the 123 from the URL, lookup a value on a database within the website and then redirect them to a different URL?

Is this something I would have to use PHP/Javascript or another technology?

  • 写回答

1条回答 默认 最新

  • du512926 2018-06-13 17:46
    关注

    I have implemented a solution similar to what you are asking for.

    There are two parts to my solution.

    Known redirects are handled by the server in the web.config file. By maintaining the web.config file with known redirects, the server will automatically redirect to the "correct" page. This requires an IIS server, but other server technologies should have similar functions (see '.htaccess Syntax').

    web.config Syntax

    <rewriteMaps>
       <rewriteMap name="redirects">
          <add key="/123" value="/new-location" />
       </rewriteMap>
    </rewriteMaps>
    

    .htaccess Syntax

    Redirect /index.html http://example.com/newdirectory/
    

    The other part of the solution is to create a custom 404 page that can access your database and manually redirect the user.

    PHP / mySQL

    <?php
       $db = mysqli_connect('YOURHOST','YOURDATABASE','YOURDATBASEPASSWORD','YOURDATABASEUSER');
       $requestedURL = $_SERVER['REQUEST_URI'];
       $sql = "SELECT redirectURL FROM tblurls WHERE requestURL LIKE '%" . $requestedURL . "%'";
       $result = mysqli_query($db,$sql);
       if(mysqli_num_rows($result) == 0) {
          // no matches
       }
       else {
          $row = $result->fetch_assoc();
          echo "<a href='" . $row['redirectURL'] . "'>Click to go here.</a>";
       };
    ?>
    

    In IIS I have setup a custom 404 system using mySQL and PHP. Depending on your flavor of server and programming, you will want to get the requested url, check that value against your database and finally redirect to the associated value.

    JavaScript Redirect

    <script type='text/javascript'>
       window.location.href = '/newlocation/';
    </script>
    

    KEEP IN MIND - If you are concerned about search engine optimization, you may want to provide a link to the correct page rather than automatically directing the user. Search engines don't like automatic redirects.

    Hope that helps.

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装