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

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图