douqu2481 2016-02-21 05:33
浏览 69
已采纳

PHP获取请求的URL htaccess mod-rewrite的目标文件路径

I use mod-rewrite to make pretty URLs, like http://example.com/users/Dan etc.

I need to fetch the rewrite rule for the supplied URL to find the path for the server-side file that is requested; however, this is not taking place on the active page so I cannot use $_SERVER['REQUEST_URI'], $_SERVER['PHP_SELF'], or ___FILE____.

I need to do something like this:

$url = '/users/Dan';
$path = $_SERVER['DOCUMENT_ROOT'].'/'.get_filepath_from_htaccess($url);

In this case, if you go to http://example.com/users/Dan you are actually viewing http://example.com/user.php?name=Dan

So, in this case, get_filepath_from_htaccess() should return /user.php?name=Dan.

I thought about running through the .htaccess file with a regex, but many of the rewrites are dynamic so it would be quite messy I imagine and I'm hoping there's a cleaner solution.

UPDATE I realize that my original question was not precise enough and my intentions were not clear. To be clear, this is exactly what the script will be used for.

On my site, users can create custom URLs and point them to existing URLs. For example, my profile would be located at example.com/users/Dan. But, when I create a custom URL (example.com/DansCustomURL) and point it to my profile, it should fetch the page contents from the profile without forwarding to example.com/users/Dan

So, here's how it goes.

A 404 first redirects to url.php which scans the database for custom URLs matching the requested URL. If one is not found, the 404 page is displayed instead.

If a file is found, url.php needs to determine the filepath for the target URL.

  • 写回答

1条回答 默认 最新

  • dongshang1529 2016-02-21 06:50
    关注

    Ok So I am making some assumptions here:

    1. You have a .htaccess file which have some rewrites, like the one you have shown in your question.
    2. Each redirect may lead you to a different page like users.php, admin.php, xyz.php..

    I think the information that you are looking for i.e /user.php?name=Dan can be extracted through $_SERVER.

    So now what you will need is the get_filepath_from_htaccess function, and this function should be available on all of your files..

    If you have a common file like config.php, functions.php, which is available(included or required) in all of the pages like user.php, admin.php etc.

    Then you need to place this function in the common file, and your function could look like

    function get_filepath_from_htaccess() {
        return $_SERVER['SCRIPT_FILENAME'] . 
                (isset($_SERVER['REDIRECT_QUERY_STRING']) ? 
                    '?' . $_SERVER['REDIRECT_QUERY_STRING'] : '');
    
    }
    

    This will give the complete path from system root, so you will not have to append $_SERVER['DOCUMENT_ROOT'].

    You might have to make a few changes to the code according to your server configuration so just take a dump(print_r or var_dump) of $_SERVER and extract out the information that you require and update the above function

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历