【问题标题】:How to clean url when using get method使用get方法时如何清理url
【发布时间】:2020-01-28 11:40:02
【问题描述】:

我目前有一个 product.php 页面,在 products.php 页面上显示每个产品的数据

我正在寻找一种方法来清理通过 product.php 文件显示其数据的每个项目的 URL。

当前网址“rubberholds/product.php?item=item1”

例如所需的 URL 'rubberholds/product/product-one'

products.php

<?php foreach ($limestoneItems as $item => $limestone): ?>    

<a href="product.php?item=<?php echo $item; ?>">
<img 
    src="assets/img/limestone/<?php echo $limestone[img]; ?>.jpg" 
    alt="<?php echo $limestone[alt]; ?>">
</a>

<?php endforeach ?>

product.php

<?php
  if (isset($_GET['item'])) {    
    $limestoneProduct = $_GET['item'];
    $product = $limestoneItems[$limestoneProduct];
  }
?>

数组

$limestoneItems = array(
  "item1" => array(
    title => "Product-One",
    img => "product1",
    alt => "Product1"                  
  )
);

有人对如何实现这一目标有任何建议吗?我想删除 .php 扩展名并将 '?item=item1' 替换为 $limestone[title]

编辑

我使用以下代码创建了一个 .htaccess 文件,但该文件不起作用:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^product/([0-9a-zA-Z_-]+) product.php?item=$1 [NC,L]

【问题讨论】:

  • 你必须使用 .htaccess 或类似的东西。
  • 修改products.php,以所需格式输出链接。如果您想将值作为 GET 参数接收,则需要相应地配置一些 URL 重写。 (如果你真的想使用/product.php/product-one,你可以直接从 PATH_INFO 获取值,假设你的服务器没有明确禁用它。)而且由于你不再使用数组键作为你的产品的标识符,您还必须修改从该数组获取产品的代码,您需要通过其title 搜索它。
  • rubberholds 是另一个 URL 路径段吗? (或主机名?)您的文件结构中的.htaccess 文件在哪里? (或者您希望 .htaccess 文件放在哪里?)
  • rubberholds 是 htdocs 中的根文件夹。我的 .htaccess 文件目前在这个根目录中

标签: php .htaccess


【解决方案1】:

.htaccess

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^rubberholds/product/?(.*)$ rubberholds/product.php?item=$1 [QSA]

测试数据:

$limestoneItems = [
    'item1' => [
        'title' => 'Product-One',
        'img'   => 'product1',
        'alt'   => 'Product1',
    ],
    'item2' => [
        'title' => 'Product-Two',
        'img'   => 'product2',
        'alt'   => 'Product2',
    ],
    'item3' => [
        'title' => 'Product-Three',
        'img'   => 'product3',
        'alt'   => 'Product3',
    ],
];

product.php

$title = strtolower($_GET['item']);
$data  = array_filter($limestoneItems, function ($v) use ($title) {
    $v['title'] = strtolower($v['title']);

    return in_array($title, $v);
});

if ($data) {
    $key     = array_keys($data)[0];
    $product = $limestoneItems[$key];

    var_dump($product); // Data found
} else {
    var_dump('Not found'); // Data not found
}

【讨论】:

    【解决方案2】:

    您可以按照以下步骤操作:

    1) 更改 .htaccess 文件以接受没有 .php 扩展名的文件

    RewriteEngine On
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    2) products.php

    <?php 
    $limestoneItems = array(
      "item1" => array(
        "title" => "Product-One",
        "img" => "product1",
        "alt" => "Product1"                  
      )
    );
    
    foreach ($limestoneItems as $item => $limestone) {
        echo '
    <a href="product?item='.$limestone[title].'">
    <img src="assets/img/limestone/'.$limestone[img].'.jpg" alt="'.$limestone[alt].'">
    </a>';
    }
    ?>
    

    3) 产品.php

    <?php
    $limestoneItems = array(
      "item1" => array(
        "title" => "Product-One",
        "img" => "product1",
        "alt" => "Product1"                  
      )
    );
    
    if (isset($_GET['item'])) { 
        $limestoneProduct = $_GET['item'];
        foreach ($limestoneItems as $key => $val) {
            if ($val['title'] === $limestoneProduct) {
                $product = $limestoneItems[$key];
                echo implode(" ",$product);  
            }
        }
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      在 .htaccess 文件中

      重写规则 ^([^/])/([^/])/([^/])\product-([^/])$ Rubberholds/product.php?item=item1[L]

      在您的代码中更改 products.php

      <a href='product.php/product-<?php echo $item; ?>'> 
      

      【讨论】:

        【解决方案4】:

        对于具有以下格式的给定 URL:

        http://example.org/rubberholds/product.php?item=item1
        

        将产生以下格式:

        http://example.org/product/item1
        

        在您的 .htaccess 中使用以下重写规则:

        RewriteEngine On
        RewriteRule ^product/([^/]*)$ /rubberholds/product.php?item=$1 [L]
        

        【讨论】:

          【解决方案5】:

          请查看以下步骤:

          (1) .htaccess 文件中的更改以接受没有 .php 扩展名的文件,并且随着更改,您希望在 URL 中。

          Options +FollowSymlinks
          RewriteEngine on
          RewriteCond %{REQUEST_FILENAME}.php -f 
          #Below line will perform /product(any_file_name) as product.php
          RewriteRule ^(.*)$ $1.php [NC]
          #Below line will perform /product/product-one(any_product_name) as product.php?item=product-one
          RewriteRule ^product/([a-z0-9-]+) product.php?item=$1 [NC]
          

          有关如何在 .htaccess 中编写 url 规则的更多详细信息,请访问: http://www.wbcsoftwarelab.com/wbcblog/url-rewriting-examples-using-htaccess

          (2) 产品.php 显示产品时更改 url 格式 来自:

          <a href="product.php?item=<?php echo $item; ?>">
          

          到:

          <a href="product/<?php echo $limestone[title]; ?>">
          

          (3) 产品.php 当用户点击该 url 时,您将在 $_GET["item"] 中获得产品名称,并像下面这样使用它来获取其密钥:

          if (!empty($_GET)) {
              if (isset($_GET['item'])) { 
                  $product = $_GET['item'];
                  foreach ($limestoneItems as $key => $value) {
                      if ($value['title'] === $product) {
                          $item = $key; //here you will get the item number for the product
                          $productDetails = $value; //this will be details of the item
                          echo "Item: ".$item."\n";
                          echo "Product Details: " . implode(" ", $productDetails);
                      }
                  }
              }
          }
          

          【讨论】:

          猜你喜欢
          • 2023-04-07
          • 2014-03-16
          • 2018-10-19
          • 2014-01-08
          • 2013-11-19
          • 1970-01-01
          • 1970-01-01
          • 2013-06-04
          • 1970-01-01
          相关资源
          最近更新 更多