2013-11-28 112 views
2

您好我想轉換的網頁URL像www.example.com/pictures.php?title=yyoy喜歡的東西www.example.com/page/yoyo.php更改網頁網址複製到PHP

我怎樣才能做到這一點?

www.example.com/pictures.php?title=yyoy當有人上傳圖片到我的網站時會生成這些頁面,所以我想將這些類型的網址變成類似上面顯示的示例。

因爲這些網頁不能在搜索引擎中編入索引。由於

Picture.php

<?php 
include("connection.php"); 

if(isset($_GET['title'])){ 

$page_id = $_GET['title']; 

    $select_query = "select * from save_data where Title='$page_id'"; 

$run_query = mysql_query($select_query); 

while($row=mysql_fetch_array($run_query)){ 

    $post_id = $row['ID']; 
    $post_title = $row['Title']; 
    $post_image = $row['Name']; 

$page_title = $_GET['title']; 
header('uploads/ ' . $page_title . '.php') 

?> 
<center> 
<h2> 
<a href="pictures.php?title=<?php echo $post_title; ?>"> 

<?php echo $post_title; ?> 

</a></center> 

</h2> 

<center><img src="uploads/<?php echo $post_image; ?>" /></center> 


<?php } }?> 
+1

看看怎麼了。htaccess文件的作品,它可以幫助 – tomahim

回答

2

我沒有測試過這一點,但你應該使用類似的.htaccess文件下列規則:

RewriteEngine on 
RewriteRule ^pictures.php?title=(.+)$ page/$1.php [R] 

需要注意的是,你需要mod_rewrite啓用此工作在apache2

+0

已經寫在.htaccess文件中的這段代碼,但什麼都沒有發生@Stoic – Wajahat

1

通常我wo ULD從一種形式張貼時創建一箇中間頁,但在你的情況下,你的鏈接看起來是這樣的:

www.example.com/pictures.php?title=yoyo 

所以它引導你在URL中的變量「標題」到pictures.php。

所以您pictures.php頁面使用:

$page_title = $_GET['title']; 

而當你想去yoyo.php:

header('Location: $page_title.php') 

從您的編輯該代碼將重定向頁面,但它會不顯示上次HTML:

<?php 
    include("connection.php"); 

    if(isset($_GET['title'])){ 

    $page_id = $_GET['title']; 

    $select_query = "select * from save_data where Title='$page_id'"; 

    $run_query = mysqli_query($select_query); 

    while($row=mysqli_fetch_assoc($run_query)){ 

     $post_id = $row['ID'];  //Just make sure the values between [''] match - 
     $post_title = $row['Title']; //your columns in database. 
     $post_image = $row['Name']; 
    } 

    header('Location:uploads/$post_title.php'); 
?> 

這將被排除在外:

<center> 
    <h2> 
    <a href="pictures.php?title=<?php echo $post_title; ?>"> 

    <?php echo $post_title; ?> 

    </a></center> 

    </h2> 

    <center><img src="uploads/<?php echo $post_image; ?>" /></center> 


    <?php } }?> 
+0

不明白這@ @ J2D8T – Wajahat

+0

我將編輯我的答案 – J2D8T

+0

我'picture.php頁面'看起來不是這樣,但仍然它不工作檢查我的帖子我已經更新了它@ J2D8T – Wajahat

1

這裏有個很好的答案:Masking $_GET variables of the URL when passing pages in PHP 這會告訴你如何屏蔽你的URL。

或者,您可以將這些文件上載到名爲「page /」的文件結構中的目錄中。然後,您可以使用www.example.com/page/filename訪問頁目錄內的文件(作爲下載)。 php

0

您應該使用.htaccess文件。補充一點:如果你使用像WAMP本地服務器

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^picture/([a-zA-Z0-9]+).php$ pictures.php?title=$1 
</IfModule> 

,一定要打開德mod_rewrite或阿帕奇rewrite_module並重新啓動服務器。

您還應該使用Google for Regular Expressions。 讓我知道它是否有用。

+0

沒有它不工作@ Joaquin-o – Wajahat

+0

你有一臺服務器嗎? ROR?哪一個? –

+0

我只是在.htaccess文件中的代碼,但沒有發生沒有錯誤沒有發生任何改變 – Wajahat

1

URE這一個

RewriteEngine on 
RewriteRule ^pictures.php?title=(.+)$ page/$1.php [R, L]