Add domain URL to href and src relative paths

I'm using a MVC model and I can invoke webpages with URLs like http://mywebsite.com/product/productid.html .
My folder structure is the following:

  • views - the views folder
  • js - the javascript and jquery folder
  • css - the stylesheets folder
  • images - the images folder
  • In views folder are contained web pages used to show data to the users. They can include scripts, images and stylesheets. The following snippet is incorrect

    <link rel="stylesheet" href="css/style.css" media="all" type='text/css' />
    

    since the webpage is called with the URL above, and css can't be found with a relative path. To solve the problem, I have defined a DOMAIN variable in PHP and changed the code into

    <link rel="stylesheet" href="<?php echo DOMAIN;?>css/style.css" media="all" type='text/css' />
    

    This works, but forces me to add the <?php echo DOMAIN;?> snippet to each href and src attribute on each page. Is it possible to automate it? I've tought to use the :before selector but I don't have idea how to use it in this case.

    Any ideas? Thanks in advance.


    :before only applies to CSS, so that's not of use here.

    There's really no way to do automatically add it in PHP that wouldn't be cpu-intensive, and/or require a significantly more complex setup than it sounds like you have right now. Using find-and-replace in your code editor is the best option.


    Using <?= DOMAIN; ?> <?= DOMAIN; ?> instead would be shorter, BTW. (See this question for more info)

    链接地址: http://www.djcxy.com/p/59356.html

    上一篇: 包括php脚本里面的javascript确认框

    下一篇: 将域URL添加到href和src相对路径