xFrank
V2EX  ›  PHP

PHP 里面怎么可以在 index. PHP 里面获取到子路径?

  •  
  •   xFrank · Feb 21, 2020 · 4293 views
    This topic created in 2449 days ago, the information mentioned may be changed or developed.

    假设当前服务器上只有一个 index.php 文件。 当访问: http://xxx.com ,能直接访问到 index.php ,没问题 但是访问: http://xxx.com/abc,会提示找不到网页。 有没有什么办法(做配置?特殊代码?)使访问上述带路径的网址时,访问的是 index.php ,然后在 index.php 里面获取到这个 abc 参数?

    13 replies  •  2020-02-21 17:20:26 +08:00
    troycode
    troycode
    Feb 21, 2020
    1
    这应该是设置的解析路径把,url 包含入口文件
    azoon
    azoon
    Feb 21, 2020
    2
    1.配置 rewrite
    2.$_SERVE['PHP_SELF']

    随便找个 PHP MVC 框架看看吧
    R18
    R18
    Feb 21, 2020 via Android
    3
    伪静态
    KasuganoSoras
    KasuganoSoras
    Feb 21, 2020
    4
    如果网站服务器是 Nginx,可以这样做伪静态规则,将所有请求转发到 PHP 上:

    location / {
    rewrite ^ /index.php$uri;
    }

    然后 PHP 里这样写就可以获取请求的完整地址:

    echo $_SERVER['REQUEST_URI'];
    encro
    encro
    Feb 21, 2020
    5
    一下点开了 http://xxx.com ,你猜看到什么了。。。


    4 楼正确,不过最好看你用的什么框架,可能还需要加上$is_args, $args 等参数。
    xFrank
    xFrank
    OP
    Feb 21, 2020
    6
    xampp 下怎么配置?
    U7Q5tLAex2FI0o0g
    U7Q5tLAex2FI0o0g
    Feb 21, 2020
    7
    路由
    xFrank
    xFrank
    OP
    Feb 21, 2020
    8
    jswh
    jswh
    Feb 21, 2020
    9
    # 访问: http://xxx.com/abc,会提示找不到网页。 有没有什么办法(做配置?特殊代码?)
    这个实在 http server 上做的,nginx/apache,软件不同配置方式不同,建议自己学习相关知识
    # 然后在 index.php 里面获取到这个 abc 参数?
    PHP 的外部系统相关参数都在$_SERVER 这个全局变量里面,类似$_GET 和$_POST,自己找。
    KasuganoSoras
    KasuganoSoras
    Feb 21, 2020
    11
    @xFrank #6 Apache 我不会配置,你可以搜索一下 “Apache 伪静态”
    FragmentLs
    FragmentLs
    Feb 21, 2020
    12
    .htaccess

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ /index.php [L,QSA]
    Nekonico
    Nekonico
    Feb 21, 2020
    13
    @xFrank 阿帕奇配置如下

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) index.php/$1 [L]


    @KasuganoSoras 哈哈小樱~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1597 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 23:42 · PVG 07:42 · LAX 15:42 · JFK 18:42
    ♥ Do have faith in what you're doing.