0

I am trying to get clean URLs of localities belong to associated district where a clean url function is applied to $_GET varialble. I've two pages, 1st one is district.php who has links to associated localities using this function to generate clean URLs. Below is the code of district.php page :-

<?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']; 
    }
?>

On the same district page, below code is used to generate clean URLs of associated localities :-

<a href=\"/".str_replace($remove, "-", clean(strtolower($row['locality'])))."-".$row['id']."\">" . $row["locality"]. "</a>

The above code is generating URLs to all localities pages in the manner of "locality-page-url-with-decoded-id-number" on district page.

Below is the code of locality.php where I'm trying to apply same clean function to $_GET variable in order to get clean URLs

<?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'];
    }
}
?>

htacces code :-

RewriteRule ^([^/]*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]

Now the problem I'm facing is that the $locality = clean($_GET['locality']); is not working here and is returning to error 404. It is not getting cleaned values of localities. When I try the same manually after removing clean function from $_GET variable using all characters available in the field, it works fine. Is there any way that I can use ID as the main variable in order to get row values having locality's clean values in the URL of the page?

  • can you provide a sample url that you're trying to get it work? – vnt Oct 01 '17 at 09:23
  • @vnt sample URL being generated on district page is :- `http://domainname.com/text-text-text-Mjk2` This is a cleaned URL. The uncleaned version of the same is :- `http://domainname.com/text.-&-text-text-Mjk2` – Vikas Kukreja Oct 01 '17 at 09:28
  • I found applied "clean" function on https://stackoverflow.com/questions/14114411/remove-all-special-characters-from-a-string but unfortunately not able to comment on this page for asking how to apply this on $_GET variable due to low reputation score at the moment. – Vikas Kukreja Oct 01 '17 at 09:35
  • I don't see any problem from your code except .htaccess. It should be `RewriteRule ^([^/]*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]` as you said the second file is `locality.php` – vnt Oct 01 '17 at 10:04
  • @vnt sorry my bad while asking the question. The file name is already locality.php in htaccess and it didn't work. This why the uncleaned version of the URL is working fine after removing the clean function from locality page. I'll fix the question. Thanks for pointing that out. – Vikas Kukreja Oct 01 '17 at 10:08

1 Answers1

0

I finally fixed it by working around the code. Here is the new htaccess code making first parameter optional and picking ID as the main parameter to work properly:-

RewriteRule ^(.*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]

Changes done on locality.php

Minor changes are done to use ID to fetch the rows in the following manner :-

<?php 
include($_SERVER['DOCUMENT_ROOT'].'/database-path.php');
$localID =  base64_decode($_GET['id']) ;
$sql="SELECT * FROM table_name WHERE 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'];
    }
}
?>

Thank you everyone for your help.