【问题标题】:modify url via htaccess passing and capturing two parameters通过 htaccess 传递和捕获两个参数修改 url
【发布时间】:2021-05-20 21:16:04
【问题描述】:

访问archive.php页面时需要通过url传递两个参数。 URL 与 sitename.com/archive.php?page="2"&category="car" 相同。 我希望 htaccess 变成这样 网站名称.com/car/2/ 接下来我想捕获这两个参数。 谁能向我解释如何编辑 htaccess 文件以及如何从 URL 中捕获两个参数。 我目前的htaccess如下:

RewriteEngine ON
Options -Indexes -MultiViews

DirectoryIndex home.php

RewriteRule ^sitemap.xml ./sitemap.php [L,QSA]


RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ core.php [L]

页面core.php:

<?php include './include/include.php'; ?>
<?php require './config.php'; ?>
<?php 
$url = basename(strtolower($_SERVER['REQUEST_URI']));
$cleanUrl = filter_var($url, FILTER_SANITIZE_STRING);

$st2 = $PDO->query("SELECT * FROM sb_article WHERE url = '$cleanUrl'");
while($recordArt = $st2->fetch()){
    $artUrl = $recordArt['url'];
    }
if($artUrl !== $cleanUrl){
$st = $PDO->query("SELECT * FROM sb_category WHERE categoria = '$cleanUrl'");
while($recordCat = $st->fetch()){
    $catTit = $recordCat['categoria'];
    }
}

switch($cleanUrl){
    case $artUrl:
        single();
    break;
    case $catTit:
        echo "sono una categoria";
    break;
    case "contatti":
        echo "sono una la pagina contatti";
    break;
    default:
    echo "articolo non trovato";
    break;
}


function single(){
    $urlArticle = basename(strtolower($_SERVER['REQUEST_URI']));
    require './include/include.php';
    require './classes/class.single.php';

    $single= new showArticle($urlArticle, $PDO);
    $showSingle = $single->single();

    if($showSingle !== NULL){ foreach($showSingle as $list){
        $id = $list['id'];
        $titolo = $list['titolo'];
        $testo = html_entity_decode($list['testo']);
        $descrizione = $list['descrizione'];
        $categoria = $list['categoria'];
        $url = $list['url'];
        $img = $list['img'];
        $data = $list['data'];}}

    require( TEMPLATE_PATH . "/single.php");
}
?>

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    我无法解决问题。网址没有更改。我试图更好地解释自己。 我有这个地址

    localhost/simpleblog/archive.php?Page=2&category=car

    我想把它转换成

    localhost/simpleblog/archive/2/car

    我目前的 htaccess 是这样的:

    RewriteEngine ON
    Options -Indexes -MultiViews
    
    DirectoryIndex home.php
    
    RewriteRule ^sitemap.xml ./sitemap.php [L,QSA]
    
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ core.php [L]
    

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 2011-12-25
      • 1970-01-01
      • 2018-03-07
      • 2022-01-25
      • 2014-05-22
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多