xFrank
V2EX  ›  PHP

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

  •  
  •   xFrank · Feb 21, 2020 · 3951 views
    This topic created in 2282 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
        1
    troycode  
       Feb 21, 2020
    这应该是设置的解析路径把,url 包含入口文件
    azoon
        2
    azoon  
       Feb 21, 2020
    1.配置 rewrite
    2.$_SERVE['PHP_SELF']

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

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

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

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


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

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

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


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