2012-09-21 30 views
0

我需要從URL中獲取值,而服務器不顯示頁面。例如,如果有人去:從url中獲取值而不顯示PHP中的頁面

www.example.com/123456

我想要得到 「123456」,並重定向到www.example.com。

www.example.com/123456不存在。

這可能與mod重寫和PHP以某種方式?

+1

不只是以某種方式,它是。檢查國防部重寫文件和教程。 – dbf

回答

1

例如,在你的.htaccess文件(假設與mod_rewrite的LAMP):

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
RewriteRule ^.*$ index.php [NC,L] 

而且index.php文件:

<?php 
    $url = $_SERVER['REQUEST_URI']; 
    // will output: /123456 
    echo $url; 
?> 

這個設置會將所有不指向實際文件或目錄的請求重定向到index.php。

+0

完美!謝謝! – pgtips

1

你可以做簡單:

<?php 
$data = $_GET['data']; 
//Do something with the data 
header('Location: http://www.example.com/'); 
?> 
+0

這將假定www.example.com/123456存在。它沒有。 – pgtips

相關問題