How to point links to site root without using . htaccess

Asked

Viewed 107 times

-2

How do I point to the root directory without needing me to put index.php in the links and without using the .htaccess ?

Example:

<a href="index.php" class="nav-brand"><img src="logo.png" alt="Logo" /></a>

In the example above, the logo that appears on all pages has the link pointing to the main page and I wanted to point without having to call index.php on the link, has some way to do this without having to use the .htaccess ?

  • Have you tried using the $_SERVER['DOCUMENT_ROOT']?

  • @Taffarelxavier No, but this global variable is dependent on Apache is not ? She’s not so recommended for certain things, I could use her in case of link pointing ?

  • According to this answer, yes: https://stackoverflow.com/questions/13394924/php-serverdocument-root

  • Machado, what’s the source? Because, according to the official website, there are comments with expressive votes with the use of it: https://www.php.net/manual/en/reserved.variables.server.php#112425

1 answer

1


Translated PHP $ _SERVER ['DOCUMENT_ROOT']:

In this kind of scenario, it’s always best to create a file config.php and save it in the root directory. In the configuration file, you set some parameters.

Call this configuration file on all pages. Your configuration file may be similar to the below.

define('APP_NAME','nome-do-projeto'); //dentro da pasta htdocs (xamp) ou da www (wamp)  
define('HTTP_SERVER', 'http://localhost/'); 
define('SITE_NAME', 'http://localhost/');   
define('DOCUMENT_ROOT',$_SERVER['DOCUMENT_ROOT'].APP_NAME);

You can also set your directory to images, css, etc. that you think will be used in various places.

So you can call it that:

<a href="<?php echo HTTP_SERVER.'index.php';?>" class="nav-brand"><img src="<?php echo HTTP_SERVER.'logo.png';?>" alt="Logo" /></a>
  • How I later load this configuration file into the pages ?

  • Usa include or require.

  • And what is the ideal method to include this configuration file, using include, require, include_once or require_once ? And you can give me an example of where to put this ?

  • There are differences between them, but basically they all do the same thing. You can use include. But you can take a look, right here, the difference between them.

  • 1

    You can always put it on top of the files php or you can use a autoload.

  • I managed to put the include at the top calling the php file as you reported and gave it right, thank you very much!

  • In the links I did as follows: <a href="<?php echo HTTP_SERVER.APP_NAME; ?>" class="nav-brand"><img src="<?php echo HTTP_SERVER.APP_NAME; ?>/logo.png" alt="Logo" /></a>. As I am using Easyphp to develop a project, it is being played from the Pendrive, so in the panel I created a project to be able to run directly from the Pendrive and not from the root.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.