How to create multiple rewrite rules using php?

php

#1

I have three files in same location as index.php, package.php and book_package.php

As, I want to make url user friendly and I am able to succeed it by creating a url in index.php files as on post request:

if(isset($_POST["packagestring"])){
    $retvalue=$_POST["packagestring"];
     $_SESSION["package_string"] = $retvalue;
    header("location:package/".php_slug($retvalue)."");
}
function php_slug($string)  
{  
    list($packageid, $packagename) = explode(":",$string);
  
   $url = strtolower(str_replace(' ', '-', $packagename))."/".$packageid;
  $slug = preg_replace('/[^a-z0-9-]+/', '-', trim(strtolower($url)));  
  return $url;  
}

and using the get method to retrieve the values as in package.php

if(!isset($_GET["packagestring"])){
 header('location:index.php');
}elseif (isset($_GET["packagestring"])) {
  $getpackage =$_GET["packagestring"];
  var_dump($getpackage);
   list($packagename, $packageid) = explode("/",$getpackage);
}

Here is the .htaccess file code to achieve the url like

http://localhost/xyz/package/glimpse-of-abc/2

RewriteEngine On  
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images)/(.*)$ $1
RewriteCond %{REQUEST_URI} package
RewriteRule ^package/([a-zA-Z0-9-/]+)$ package.php?packagestring=$1
RewriteRule ^package/([a-zA-Z0-9-/]+)/ package.php?packagestring=$1

Its Okay upto here.
I am in second file now and want to jump to third file via POST method with new url

Here is the code for makeing url in package.php

  function php_slug($string)  
    {  
        list($packageid, $packagename) = explode(":",$string);
      
       $url = strtolower(str_replace(' ', '-', $packagename))."/".$packageid;
      $slug = preg_replace('/[^a-z0-9-]+/', '-', trim(strtolower($url)));  
      return $url;  
    } 

  if(isset($_POST["booknow"])){
   
        $retvalue=$_POST["booknow"];
       
        header("location:book_package/".php_slug($retvalue)."");
        
    }

and book_package.php to get the values as:

if(!isset($_GET["booknow"])){
     header('location:index.php');
 }elseif (isset($_GET["booknow"])) {
      $getpackage = $_GET["booknow"];
      var_dump($getpackage);
    
 }

Below is the full .htaccess code:

RewriteEngine On  
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images)/(.*)$ $1
RewriteCond %{REQUEST_URI} package
RewriteRule ^package/([a-zA-Z0-9-/]+)$ package.php?packagestring=$1
RewriteRule ^package/([a-zA-Z0-9-/]+)/ package.php?packagestring=$1


RewriteCond %{REQUEST_URI} book_package
RewriteRule ^book_package/([a-zA-Z0-9-/]+)$ book_package.php?booknow=$1  
RewriteRule ^book_package/([a-zA-Z-0-9-]+)/ book_package.php?booknow=$1 
RewriteRule ^xyz/(css|js|img)/(.*)?$ /$1 [L,QSA,R=301]

But when I try to redirect to book_package.php i got the below url:
http://localhost/xyz/package/glimpse-of-abc/book_package/glimpse-of-abc/2

and not able to redirect the book_package.php page

I also tested it manually as:
http://localhost/xyz/book_package/glimpse-of-abc/2

then I am able to jump tp book_package.php page with no error.

I think that I only need to clear or clean the package url before redirecting to book_package page and I am not able to achieve it.

Any one suggest me that how to achieve it

Many Thanks


#2

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.