简体   繁体   中英

Clean URL with PHP on XAMPP

I have been struggling for months now, and I'm just not getting it. I'm trying to get clean urls with php on xampp, I either get server error 500 or www.something.com/root/index.php?page=whatever does not go away I want www.someting.com/page/queryresult/

Can anyone help?

I think the problem is linking the menu to the query and the sticky part is in the switch

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", "", ""); }

//This part Connects the menu to the Database Query Where ?Page= the Data Value connection

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

        }
    echo '</ul>';


    }

 }

add a file called .htaccess in your home directory

start the rewrite engine at the top of the file:

<IfModule mod_rewrite.c>
RewriteEngine On

In that file, you'll write some conditions for re-directing to a clean 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]

This is a bit confusing, but basically what it means is that when your browser receives the ugly URL, it gets redirected to the pretty URL. Then, the re-write fills the pretty URL with the content from the ugly URL.

Close your if statement after you're done re-writing:

</IfModule>

i have xampp on windows;

.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"

What i do is i use file_exists to use $vars[0] and $vars[1] to reference a folder/file. if the file doesnt exist, route to $vars[0]/index.php

if the folder doesnt exists, redirect to index.php

Hope this helps to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM