How do I get the files to be pulled from the root of the site?

1

I'm creating a website ... So far so good.

I've created friendly URLs.

Before my URL was something like:

localhost/site/produto.php

And now it's:

localhost/site/produtos/produto/planos

I wanted a code PHP to pull all links from the root of the site.

  

Ex: Before my site pulled an image with the name imagem.png in the imagens/imagem.png case, but after changing my URL, I now have to use ../../imagens/imagem.png because if it does not do this in the images for example, it will give all Error 404 .

Is there a way to make it change the url of the directory automatically without having to edit one by one?

In case it would be:

    href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/imagens/imagem.png"

Now comes the problem: How to put the code above <?php echo $_SERVER['SERVER_NAME']; ?> automatically in all images via PHP ?

    
asked by anonymous 18.12.2014 / 03:31

2 answers

5

You can set yourself two ways

1º - using "/" before the links. Ex.:

<img src="/images/uploads/upload-10-50-100.png" alt="" />

2º - Create a subdomain with your scripts, images and css. Ex.:

<img src="http://assets.seusite.com.br/images/upload-10-50-100.png"alt="" />

There are more options and certainly a tool for this, but in any case, use the first option if you do not want to point to a subdomain.

    
18.12.2014 / 12:14
5

A cool way is to have a constant defined by having the root path of the site. For example:

define("SITE", "http://menusite.com.br/");

There in the images would look like this:

<img src="<?= SITE ?>imagens/minhaimagem.png" alt="" />

The cool of this approach is that it will work perfectly on localhost as well, since you can set the constant to the directory you want, for example, http://localhost/meusite/ . As for editing one by one your references in the code can not get away, but any IDE has a functionality to perform a general replace on the project.

    
19.12.2014 / 12:16