0

我知道这个问题在很多地方都被问过,但我想要达到的目标有点不同。

设想:

我有一个 /hotel/index.php 页面,该页面使用变量“id”从 MySQL 数据库中提取酒店数据。

我想要实现的是例如 /hotel/index.php?id=1 应该显示为 /hotel/hotelname/

这是我的 index.php 的样子:

 <?php

 $dbhost = something;
 $dbuser = somethingmore;
 $dbpass = alotmore;
 $hid = $_GET["id"];

 $conn = mysql_connect($dbhost, $dbuser, $dbpass);
 if(! $conn )
 {
   die('Could not connect: ' . mysql_error());
 }
 $sql = 'SELECT * FROM hotel where id = .$hid';

 mysql_select_db('h_db');
 $retval = mysql_query("SELECT * FROM hotel WHERE id='". mysql_real_escape_string( $hid ) ."'", >$conn );

 if(! $retval )
 {
   die('Could not get data: ' . mysql_error());
 }
 while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
 {

    if($row['inventory_source']="T") // Check inventory source
  {
 $hotelid = $row['hotelid_t'];
  }
  else
  {
 $hotelid = $row['hotelid_b'];
  }
         $hotelname = $row['hotel_name'];
         $thumbnail = $row['thumbnail'];
         $hoteladdress = $row['hotel_address_full'];
         $cityname = $row['city'];
         $cityid = $row['cityid'];
         $country = $row['country_name'];
         $propertydesc = $row['property_description'];
         $amenities = $row['xmlamenities'];
         $checkin = $row['checkin'];
         $checkout = $row['checkout'];
         $petsallowed = $row['pets_allowed'];
         $guestrating = $row['overall_rating'];
         $star = $row['star_rating'];
         $roomcount = $row['room_count'];

 }

我知道这可以使用 mod_rewrite 来实现。但我不知道该怎么做。专业人士的任何帮助将不胜感激。

4

2 回答 2

0

制作“.htaccess”文件并将此代码放入其中:

IndexIgnore *
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^hotel\/([^\\/]*)$ /hotel/index.php?id=$1 [NC,QSA]

在 index.php 中,您应该按酒店名称搜索 mysql 表。

$retval = mysql_query("SELECT * FROM hotel WHERE hotel_name='". mysql_real_escape_string( $id ) ."'", >$conn );
于 2013-07-16T06:15:48.203 回答
0

做这样的事情:

首先,您可能会弄清楚如何检测是否需要重定向。如果你这样做了:

 //$indexpage = this is an index.php?id=?? page
 if($indexpage){
    //insert your "find hotelname code"
    $hotelname = $row['hotel_name'];
    //make sure NO output is above this line!
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://www.yoursite.com/hotel/{$hotelname}"); 
  }else{
     //not an index page, so a hotel/hotelname page? (room for other pages here)
     //add code to find the hotel and show stuff. here. basically, your old page.
  }

有趣的部分是您的旧页面将被重定向,但您的真实网址将是新页面,因此您可以调用这些/链接那些 :)

于 2013-07-16T06:15:52.307 回答