如何使用php和htaccess为diff页面编写多个url

时间:2018-03-19 12:28:30

标签: php .htaccess

我的网页需要多个网址重写方法。 我可以使用PHP和htaccess进行一次url重写: index.php页面代码 PHP代码:

       $retvalue=$_POST["packagestring"];
     $_SESSION["package_string"] = $retvalue;
    header("location:package/".php_slug($retvalue)."");
}
function php_slug($string)  
{  
    list($packageid, $packagename) = explode(":",$string);

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

当我点击按钮时,通过索引页面的post方法,我跳转到包含页面的网址为: http://localhost/xyz/package/2/temp-temp-temp

因为这个包页面包含一个提交按钮,当我点击这个usbmuit按钮时,我想去book_package页面,其中包含以下网址:

http://localhost/xyz/book_package/2/temp-temp-temp

但我得到了 http://localhost/xyz/package/2/book_package.php

这是带有url的工作htaccess文件,而不是第二个url

RewriteEngine On  
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images)/(.*)$ $1
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^package/([a-zA-Z0-9-/]+)$ package.php?packagestring=$1  
RewriteRule ^package/([a-zA-Z-0-9-]+)/ package.php?packagestring=$1 
RewriteRule ^xyz/(css|js|img)/(.*)?$ /$1 [L,QSA,R=301] 
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
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] 

以下是网址重写的包页面代码:

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

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

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

任何人都建议我如何实现这个

1 个答案:

答案 0 :(得分:0)

我有3页作为 index.php 2. package.php 3. book_package.php我希望以1-> 2-> 3的顺序遍历 索引页面部分代码为:

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 = $packageid."/".strtolower(str_replace(' ', '-', $packagename));
  $slug = preg_replace('/[^a-z0-9-]+/', '-', trim(strtolower($url)));  
  return $url;  
} 

ON POST请求,我得到packagetring值为" 2:temp temp temp" 现在,我想用用户firendly url跳转到package.php,这就是为什么我创建了一个slug函数,将packagestring转换为2-temp-temp-temp,location将是package.php

htaccess文件代码是:

RewriteEngine On  
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images)/(.*)$ $1
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^package/([a-zA-Z0-9-/]+)$ package.php?packagestring=$1  
RewriteRule ^package/([a-zA-Z-0-9-]+)/ package.php?packagestring=$1 
RewriteRule ^xyz/(css|js|img)/(.*)?$ /$1 [L,QSA,R=301] 
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
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] 

上面的代码将帮助我制作用户友好的网址 本地主机:// XYZ /包/ 2-TEMP-TEMP-TEMP 而不是localhost://xyz/package.php?packagestring = 2:temp temp temp 通过将package.php文件中的packagestring作为:

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

这里很好,这意味着package.php页面现在,我想跳转到book_package.php,下面是package.php POST代码,当我点击包书按钮时:

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

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

及以下是获取字符串值的book_package代码:

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

这次请求的文件是book_package,上面提到了htaccess代码..

在这里,我无法跳转到book_package.php页面 我在同一页面(package.php),但新网址为:

http://localhost/xyz/package/2/book_package.php

我想要的     http://localhost/xyz/book_package/2/temp-temp-temp

我认为这没关系