简体   繁体   English

URL重写和GET参数

[英]URL rewrite and GET parameter

I'm using htaccess url rewrite only for index.php and using PHP to capture the GET parameter. 我仅对index.php使用htaccess url重写,并使用PHP捕获GET参数。 When trying different parameter my website is giving 404 error on certain parameter which I found very strange. 当尝试不同的参数时,我的网站在某些参数上给出了404错误,我觉得很奇怪。 However below are the details: 但是,以下是详细信息:

htaccess htaccess

RewriteRule ^([^/index.php]*)$ /index.php?company=$1 [L]

index.php index.php

<?php
$name = 'specialpromo';

if(isset($_GET['company'])) {
    include 'include/dbconfig.php';  

    // Create connection
    $conn = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$conn) {
        $errmsg = "Connection error";
    } else {
        if(!(mysqli_select_db($conn,$dbname))){
            $errmsg = "Database error";
        } else {
            $sql_comp = $conn->prepare("select a.* 
            from company a
            where a.code = ?");
            $sql_comp->bind_param('s', $_GET['company']);
            $sql_comp->execute();
            $rs_comp =  $sql_comp->get_result() or die('Error, query failed<br />');

            $numrow_comp = mysqli_num_rows($rs_comp);

            if ($numrow_comp==1){
                if($row_comp = mysqli_fetch_assoc($rs_comp)){
                    $name = $row_comp['name'];
                }
            }

            mysqli_free_result($rs_comp);
        }
        mysqli_close ($conn);
    }
}
?>

Please try the URL below as both values are not in my database, they are giving weird result. 请尝试使用以下网址,因为这两个值均不在我的数据库中,它们给出了奇怪的结果。

  1. https://shop.specialpromo.asia/ddd will return error 404 https://shop.specialpromo.asia/ddd将返回错误404
  2. https://shop.specialpromo.asia/aaa will load just fine although no result found https://shop.specialpromo.asia/aaa将加载正常,尽管未找到结果
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?company=$1 [L]

Line 1: if request uri does not match any existing directory 第1行:如果请求uri与任何现有目录都不匹配

Line 2: if request uri does not match any existing file 第2行:如果请求uri与任何现有文件都不匹配

Line 3: then for every request uri, rewrite to index.php?company=$1 , $1 is the backreference to the regex (.*) 第3行:然后对于每个请求uri,重写为index.php?company=$1$1是对正则表达式(.*)的反向引用(.*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM