V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xFrank
V2EX  ›  PHP

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

  •  
  •   xFrank · 2020-02-21 15:41:39 +08:00 · 2996 次点击
    这是一个创建于 1497 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

    echo $_SERVER['REQUEST_URI'];
    encro
        5
    encro  
       2020-02-21 16:00:40 +08:00
    一下点开了 http://xxx.com ,你猜看到什么了。。。


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

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

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


    @KasuganoSoras 哈哈小樱~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5393 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:57 · PVG 14:57 · LAX 23:57 · JFK 02:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.