【问题标题】:get value from url without displaying page in PHP从 url 获取值而不在 PHP 中显示页面
【发布时间】:2012-09-14 06:24:23
【问题描述】:

我需要在服务器不显示页面的情况下从 url 获取值。例如,如果有人去:

www.example.com/123456

我想获取“123456”并重定向到 www.example.com。

www.example.com/123456 不存在。

通过 mod rewrite 和 PHP 可以实现吗?

【问题讨论】:

  • 不只是以某种方式,它是。检查 mod rewrite 文档和教程。

标签: php url mod-rewrite


【解决方案1】:

例如在您的 .htaccess 文件中(假设 LAMP 带有 mod_rewrite):

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。

【讨论】:

    【解决方案2】:

    你可以这样做:

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

    【讨论】:

    • 这将假定 www.example.com/123456 存在。它没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2020-05-20
    相关资源
    最近更新 更多