drwf69817 2016-06-03 21:16
浏览 45
已采纳

SEO URL与多个files.php

I have many hours trying to find the solution here but none looks like what I'm looking for ... I will give a long explanation and possibly someone will serve you what I have and not need to change it.

Table pages

enter image description here

Table products

enter image description here

File .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?route=$1&url=$2 [NC,L]

If I put in the browser: example.com/pages/contact

really we are in: example.com/pages.php?route=pages&url=contact

In the file pages.php

<?php require_once 'inc/header.php'; ?>
 <section>
  <div class="container">
   <div class="row">
    <?php
    $route= $_GET["route"];
    $url = $_GET["url"];
    $results = DB::query("SELECT * FROM $route WHERE url='$url'");
    foreach ($results as $row) { ?>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <h2><?php echo $row['name']; ?></h2>
   </div> 
   <?php } ?>
   </div>
  </div>
 </section>
<?php require_once 'inc/footer.php'; ?>

The same goes for products.php, is the same code as all get by _GET

The problem I have:

I want to eliminate the $route in the URL, I want the URL looks like this:

example.com/contact or example.com/acer-one

Is how to do this, if the two designs are identical, ie, put everything in index.php, the problem is I need to use the two files to display a design or another.

Where do I begin? I modified it? It is a project I'm starting from scratch, so I can change anything.

  • 写回答

1条回答 默认 最新

  • dongshan4518 2016-06-03 21:59
    关注

    Have a look at the following example structure:

    Start by adding a table "routes" that just contains each possible url, along with a type (and every field that both pages and products have in common). This would also mean you can remove the url column (and the other common columns) from the pages and products table (or perhaps remove the table all together, if no relevant data is left). Something like this:

    TABLE routes
    id   route     type      name
    1    contact   page      Contact
    2    acer1     product   Acer 1
    

    Add your main controller, probably index.php, that looks something like this:

    <?php
        $url = isset($_GET["url"]) ? $_GET["url"] : 'home';
        // you should probably use prepared statements here, but that is a different topic
        $result = DB::query("SELECT * FROM routes WHERE url='$url'" LIMIT 1);
    
       if (! $result) {
         // 404
       }
    
       // any other common logic can go here, like header and stuff
       require_once 'inc/header.php';
    
       // now load the type specific page
       include $result['type'] . '.php';
    
       // and continue with the shared logic
       require_once 'inc/footer.php';
    

    Change your .htaccess to send everything to index.php

    RewriteRule ^(.*)$ index.php?url=$1 [NC,L] 
    

    Now you only need a single path element and no type in the url. And you could add a page with url home for when no path elements are provided. Plus you have the possibility to still work with deep links 'my/awesome/deep/linked/page', all you have to do as add that url to your routes table. Inventing a new page type would be very easy as well, just add a 'my-new-type.php' file for the custom logic and you are set.

    The key is to have a single point of entry into your application, a so called frontcontroller. And keep your code DRY, Don't Repeat Yourself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)