ฉันกำลังพยายามรับ URL ที่ชัดเจนของท้องถิ่นที่เป็นของเขตที่เกี่ยวข้องซึ่งมีการนำฟังก์ชัน URL ที่ชัดเจนไปใช้กับตัวแปร $_GET ฉันมีสองเพจ เพจแรกคือ District.php ซึ่งมีลิงก์ไปยังท้องถิ่นที่เกี่ยวข้องโดยใช้ฟังก์ชันนี้เพื่อสร้าง URL ที่สะอาด ด้านล่างนี้เป็นโค้ดของหน้า District.php :-
<?php
$remove[] = " ";
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
$string = preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
return $string;
}
include($_SERVER['DOCUMENT_ROOT'].'/database-path.php');
$district = str_replace('-',' ',$_GET['district']);
$state = str_replace('-',' ',$_GET['state']);
$sql="SELECT * FROM table_name WHERE district='$district' AND state='$state'";
$result = mysqli_query($con,$sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$district = $row['district'];
$locality = $row['locality'];
$state = $row['state'];
$iso = $row['iso'];
}
?>
ในหน้าเขตเดียวกัน โค้ดด้านล่างนี้ใช้เพื่อสร้าง URL ที่ชัดเจนของท้องถิ่นที่เกี่ยวข้อง:-
<a href=\"/".str_replace($remove, "-", clean(strtolower($row['locality'])))."-".$row['id']."\">" . $row["locality"]. "</a>
รหัสข้างต้นกำลังสร้าง URL ไปยังหน้าท้องถิ่นทั้งหมดในลักษณะของ "locality-page-url-with-decoded-id-number" ในหน้าอำเภอ
ด้านล่างนี้เป็นโค้ดของ locality.php ที่ฉันพยายามใช้ฟังก์ชัน clean แบบเดียวกันกับตัวแปร $_GET เพื่อให้ได้ URL ที่สะอาด
<?php
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
$string = preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
return $string;
}
include($_SERVER['DOCUMENT_ROOT'].'/database-path.php');
$locality = clean($_GET['locality']);
$localID = base64_decode($_GET['id']) ;
$sql="SELECT * FROM table_name WHERE locality='$locality' AND id='$localID'";
$result = mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($result);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$locality = $row['locality'];
$content = $row['content'];
$district = $row['district'];
$state = $row['state'];
$iso = $row['fld_iso'];
}
}
?>
รหัส httacs:-
RewriteRule ^([^/]*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]
ตอนนี้ปัญหาที่ฉันกำลังเผชิญคือ $locality = clean($_GET['locality']);
ไม่ทำงานที่นี่ และกำลังกลับไปสู่ข้อผิดพลาด 404 โดยไม่ได้รับการล้างค่าของท้องถิ่น เมื่อฉันลองแบบเดียวกันด้วยตนเองหลังจากลบฟังก์ชัน clean ออกจากตัวแปร $_GET โดยใช้อักขระทั้งหมดที่มีอยู่ในฟิลด์ มันก็ทำงานได้ดี มีวิธีใดบ้างที่ฉันสามารถใช้ ID เป็นตัวแปรหลักเพื่อรับค่าแถวที่มีค่าที่สะอาดของท้องถิ่นใน URL ของเพจ
http://domainname.com/text-text-text-Mjk2
นี่คือ URL ที่ล้างแล้ว เวอร์ชันที่ไม่สะอาดของเดียวกันคือ :-http://domainname.com/text.-&-text-text-Mjk2
- person Vikas Kukreja   schedule 01.10.2017RewriteRule ^([^/]*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]
อย่างที่คุณบอกว่าไฟล์ที่สองคือlocality.php
- person vnt   schedule 01.10.2017