douluo2930 2014-05-30 06:38
浏览 7
已采纳

在不创建页面的情况下实现页面网址的最佳方式(动态网址?)

I have been thinking for quite some time now about how I could manage thousands of webpages that change based on a set of data in MySQL.

So in essence, I have thousands of "products" and each is represented by a row in a database table.

What I do currently to page each of these "products" viewable via a url is place a .php file in the url location so that when a user visits http://www.mydomain.com/some_random_product.php they are opening one of many .php files that all contain the following:

<?php
$thisPageName = basename(__FILE__, ".php");
include '/out/of/root/allProducts.controller.php';
?>

So essentially all is hand off the url name to another script that takes the url name then searches the database for a corresponding "product", if it finds the product then it displays a product page with the relevant information and if not it displays an error page stating that the product could not be found.

What I would like to do now, is better that process so that I would not need to create thousands of physical files on the server. Even though this is quite a simple task as I have a script to do it, it just becomes a pain having to run the thing all the time (I have to do it locally as it is a resource hog, then copy all the pages across to the server).

Possibly just one file that all of the urls in that location would load without changing the url as I need it to find the relating info.

I would like to add that the server running the domain is a VPS that I have full access to and I understand that the Apache mod_rewrite module may be of use here but I honestly would not know where to even start in terms of the logic behind implementing that.

If anyone could suggest a better way to do this or anything that may be of relevance then it would be greatly appreciated, thank you!

  • 写回答

2条回答 默认 最新

  • dongliang7545 2014-05-30 07:30
    关注

    Pass the product ID in the URL and catch it in your PHP script with $_GET[productId].

    Then you should query the database to get the row of the product matching that ID and display the data of that product.

    To make the URL's pretty for this case and stay SEO friendly:

    #.htaccess
    RewriteEngine On
    RewriteRule ^product/([0-9]+)/(.*)$ /product/product.php?productId=$1&name=$2
    
    # URL:
    <a href="/product/product.php?productId=123&name=product-one">
        Product one
    </a>
    
    <!-- OR (Because of the URL rewriting) -->
    
    <a href="/product/123/product-one">
        Product one
    </a>
    
    # PHP Script
    $id = $_GET[productId];
    
    $sql = "SELECT *
    FROM `product`
    WHERE `id` = ?;";
    
    etc...
    

    This way you could show thousands of different content on just one simple PHP page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度