dt246813579 2019-03-16 18:09 采纳率: 0%
浏览 8
已采纳

文件夹名称变为php中的变量

Can i grab folder name into variable in php from browser?

For example i access this link:

example.com/folder/1928-1293

it should be inside my main

example.com/index.php

I tried before and it work with htaccess but i need to make it inside the index.php to make htaccess work with another folder.

<?php
$a = "folder";
$b = "1928-1293";
echo $a . $b;
?>

And it will showing me folder1928-1293

Thanks !

  • 写回答

1条回答 默认 最新

  • dq23171 2019-03-16 21:55
    关注

    If I understand correctly, you want to configure your site so that when someone accesses "example.com/folder/1928-1293", you want that request to be handled by example.com/index.php, and you want index.php to be able to read the path/folders in the request.

    If so, then yes, you can. However, as you mentioned, this is only something you can do with the rewrite engine (.htaccess is the usual way of implementing it). This .htaccess snippet will take all requests that do not match up to an existing folder or file, and send them to index.php:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    </IfModule>
    

    It's basically the same rewriting function from WordPress.

    This way, if someone asks for example.com/otherfolder/otherfile.jpg and that folder/file is actually a real, physical file, then the .htaccess file will not change that request. It will only route requests for folders/files that don't physically exist.

    Inside index.php, you can then see the original path/request in the $_SERVER["REQUEST_URI"] variable and split the path into pieces with explode():

    <?php
    // $_SERVER["REQUEST_URI"] = "/folder/1928-1293"
    $path_pieces = explode("/",ltrim($_SERVER["REQUEST_URI"],"/"));
    echo $path_pieces[0]; // "folder"
    echo $path_pieces[1]; // "1928-1293"
    

    You can also use the .htaccess for multiple rules, in case you need to handle more/other URLs in your .htaccess file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?