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.

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

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?