[英]URL rewrite and GET parameter
我僅對index.php
使用htaccess url重寫,並使用PHP捕獲GET參數。 當嘗試不同的參數時,我的網站在某些參數上給出了404錯誤,我覺得很奇怪。 但是,以下是詳細信息:
htaccess
RewriteRule ^([^/index.php]*)$ /index.php?company=$1 [L]
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);
}
}
?>
請嘗試使用以下網址,因為這兩個值均不在我的數據庫中,它們給出了奇怪的結果。
https://shop.specialpromo.asia/ddd
將返回錯誤404 https://shop.specialpromo.asia/aaa
將加載正常,盡管未找到結果 RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?company=$1 [L]
第1行:如果請求uri與任何現有目錄都不匹配
第2行:如果請求uri與任何現有文件都不匹配
第3行:然后對於每個請求uri,重寫為index.php?company=$1
, $1
是對正則表達式(.*)
的反向引用(.*)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.