drgdn82648 2016-12-07 15:56
浏览 27
已采纳

当我的链接标记在其href中具有相对路径时,这是一种奇怪的行为

I installed lampp recently in my linux machin, the sources files should be placed in /opt/lampp/htdocs directory which is annoying to work with... so what I did is creating a Symbolic link which point to that directory like that :

sudo ln -s /home/medBo/projects /opt/lampp/htdocs

Now I have my project in the following path : /home/medBo/projects/ecommerce/ and everything work fine

In a view I have a link like this :

<a href="/test/hello">click here</a>

what I expect from that link is to be pointed to localhost/projects/ecommerce/test/hello but when I hover the mouse over it or I click it, it send me to localhost/test/hello

Isn't localhost/projects/ecommerce/ considered as my site root ? what I can do about it ?

UPDATE :

Someone suggested in the comment to add <base herf="http://localhost/projects/ecommerce"> in the head of the page, well the issue with this is when I go to this url

localhost/projects/ecommerce/some-controller/some-action

and inside that page there is a link to /go-to-other-controller/other-action

The destination becomes localhost/projects/ecommerce/some-controller/some-action/go-to-other-controller/other-action

  • 写回答

1条回答 默认 最新

  • dpntq48842 2016-12-07 16:58
    关注

    The site root is always your domain name (example.com or in this case localhost) or the IP address.

    It isn't clear if the end of your path is generated through .htaccess.

    If it is, you can get the file of the script which is currently executed with $_SERVER["SCRIPT_NAME"]. With PHP you can now generate the path:

    $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']), array('off','no'))) ? 'https' : 'http';
    $url .= '://'.$_SERVER["HTTP_HOST"];
    $url .= (substr($_SERVER["SCRIPT_NAME"], 0, -strlen(basename($_SERVER["SCRIPT_NAME"]))));
    

    This generates the path to the script which is executed, but without the filename of the file. Now you can add the path to it (e.g. echo $url . '/path/to/url';)

    In case the path is not virtually set by .htaccess but are real directories, you may want to use $_SERVER["REQUEST_URI"] and follow the path for x amount of directories to reach the path you need, then append the new path.

    Another possibility is to use a constant which is either set dynamically by one of the options above or manually by typing in the path you need.

    In this case the two options underneath should work:

    define("ROOT_PATH", "/projects/ecommerce/");
    

    or

    define("ROOT_PATH", "http://localhost/projects/ecommerce/");
    

    Using the magic constants from PHP could solve your problem as well:

    define('ROOT_PATH', dirname(__DIR__));
    

    or for PHP prior to 5.3.0:

    define('ROOT_PATH', dirname(dirname(__FILE__)));
    

    To output the defined constant you can just use echo ROOT_PATH . '/path/to/url';.

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

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错