在XAMPP上使用PHP清理URL


Clean URL with PHP on XAMPP

我已经挣扎了几个月了,我就是不明白。我想用php在xampp上得到干净的url,要么是服务器错误500,要么是www.something.com/root/index.php?page=whatever没有消失,我想要www.someting.com/page/queryresult/

有人能帮忙吗?

我认为问题是将菜单链接到查询,而粘性部分在开关

function display_menus_new()
{
 $sql = "SELECT * FROM menus";
 $query = mysql_query($sql) or  die(mysql_error());
 $array = array();
 if (mysql_num_rows($query)){
while($rows = mysql_fetch_array($query)){
$array[$rows['parent_id']][] = $rows;
}
loop_array($array); 
}
}
  function loop_array($array = array(), $parent_id = 0)
{
  if(!empty($array[$parent_id])) {
     echo '<ul>';
      foreach($array[$parent_id] as $items){
     echo '<li>';
 switch($items['name']){ 
case 'Home': print_r('<a href="home.php" >'.($items['name']).'</a>');   
        $items = str_replace("name", "", "");
case 'About': print_r('<a href="about.php" >'.($items['name']).'</a>'); 
         $items = str_replace("name", "", "");      
 case 'Services': print_r('<a href="services.php" >'.($items['name']).'</a>');  
        $items = str_replace("name", "", "");
  case 'Contact': print_r('<a href="contact.php" >'.($items['name']).'</a>');   
        $items = str_replace("name", "", ""); }

//这部分将菜单连接到数据库查询Where ?Page= Data Value连接

         print_r('<a href="?Page='.($items['name']).'" >'); 
    echo  $items['name'];
    loop_array($array, $items['Cat']);
echo '</a></li>';
        }
    echo '</ul>';

    }
 }

我在windows上有xampp;

. htaccess:

RewriteEngine On
# Run everything else but real files through parse.php
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI}  !^/dev-boards
# RewriteCond %{REQUEST_URI}  !^/tests
#RewriteCond %{HTTP_HOST} !^(admin|admintemplate)'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !'.(css|png|html|js|json|jpg|jpeg)$
RewriteRule ^(.*)$ parse.php?info=$1 [L]

parse.php:

<?php
include("../connect.local.php");
session_start();
$getVars = $_GET['info'];
$vars = explode("/",$getVars);
//http://localhost/viewprofile/0/yes
//$vars[0] is "viewprofile"
//$vars[1] is 0
//$vars[2] is "yes"

我所做的是使用file_exists来使用$vars[0]和$vars[1]来引用文件夹/文件。如果文件不存在,路由到$vars[0]/index.php

如果文件夹不存在,重定向到index.php

在主目录下添加一个名为.htaccess的文件

在文件的顶部启动重写引擎:

<IfModule mod_rewrite.c>
RewriteEngine On
在该文件中,您将编写一些重定向到干净URL的条件:
#first, we need to define the request, in this case, index.php?page=whatever
RewriteCond %{THE_REQUEST} /index.php?page=([0-9]*)
# now we need to make it look like it's just the clean url
RewriteRule ^$ /page/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9]+) /index.php?page=$1 [L]

这有点令人困惑,但基本上它的意思是当你的浏览器接收到丑陋的URL时,它会被重定向到漂亮的URL。然后,重写用丑URL的内容填充漂亮的URL。

写完后关闭if语句:

</IfModule>