0

この質問が多くの場所で尋ねられたことは知っていますが、私が達成しようとしていることは少し異なります。

シナリオ:

変数「id」を使用してMySQLデータベースからホテルデータを取得する/hotel/index.phpページがあります。

私が達成したいのは、たとえば /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.
  }

楽しい部分は、古いページがリダイレクトされることですが、実際の URL は新しいページになるため、それらを呼び出したりリンクしたりすることができます :)

于 2013-07-16T06:15:52.307 に答える