0

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.

Giorgio
  • 1,940
  • 5
  • 39
  • 64
  • why can't CSS be found with a relative path? – in need of help Mar 12 '14 at 15:23
  • It's because I am invoking the webpage from an url like `http://mywebsite.com/product/productid.html`. Product is a virtual folder, created with MVC model and .htaccess file. By means of URL, it's at the same level of `css` folder, so if I have a file inside it, I cannot write relative paths: it doesn't actually contain a `css` folder. Hope it's clearer now, otherwise please ask. – Giorgio Mar 12 '14 at 15:30

1 Answers1

1

: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; ?> instead would be shorter, BTW. (See this question for more info)
Community
  • 1
  • 1
Mooseman
  • 18,763
  • 14
  • 70
  • 93