2012-09-21 29 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

相关问题