0

I use xampp on my local and I work on different projects. I generally use included top menus on web sites.

Directory Example:

C:\xampp\htdocs\site1

C:\xampp\htdocs\site2

In an included top menu I give links as

<a href="/site1/page1">Link</a>
<a href="/site1/page2">Link</a>
....
<a href="/site1/page7">Link</a>

But when I upload the site on a remote server, this links doesn't work. Because the root folder is not htdocs. The root folder is site1 or site2 whatever..

While I am working on site1, on my local, I have to set my default root directory to "site1" and I give links as

<a href="/page1">Link</a>
<a href="/page1">Link</a>

and it works for both (local-remote)

Question: How can I give the links in an included top menu without changing the local root directory.. I have tried as ../page1. For main pages it worked but for sub pages didn't work..

vishuB
  • 4,173
  • 5
  • 31
  • 49
Softcore
  • 95
  • 1
  • 4
  • 13
  • 1
    if you start your link with / it moves to the root directory and goes on from there. To get the path from your active directory put ./ in. In your example Link – KeepAlive Sep 23 '15 at 11:41
  • I didn't write in example but I have directories. With ./directory it works. When I click this link, all link becomes localhost/site1/directory/directory....... – Softcore Sep 23 '15 at 12:05
  • then you might want to look at $_SERVER['PHP_SELF'] to get the base path of your php file and maybe navigate back in code (analysing output of variable) http://stackoverflow.com/questions/3602323/trying-to-get-a-full-url-without-filename-in-php This link might help you as well – KeepAlive Sep 23 '15 at 12:15

2 Answers2

0

use

<a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/page1"> Link</a>

Or Create a config.php and define path like

$localhost = true // change this to false when you upload to server
if($localhost) {
  $root_path = 'http://localhost/site1/';
} else {
  $root_path = 'http://'.$_SERVER['SERVER_NAME'].'/';
}
define('ROOT_PATH', $root_path);

And include this config.php in all your files where you needs to define the path

<?php
 include_once(dirname(__FILE__)."/config.php");
?>
 <a href="<?php echo ROOT_PATH;?>/page1">Link</a>
Arun Krish
  • 2,153
  • 1
  • 10
  • 15
0

Create a config.php file. On your remote sites this would just hold:

$path='/';

Put it in the same directory as your Menu file, in your menu file include the config.php:

<?php require_once('config.php'); ?>

and define links like this:

<a href="<?= $path ?>page1">Link1</a>
<a href="<?= $path ?>page2">Link2</a>

On your local machine edit the config file depending on the site you are working on:

$path='/site1/';
AntG
  • 1,291
  • 2
  • 19
  • 29
  • For the people who has the same problem: if you name the file as "config.php" it doesn't work. I couldn't understand the reason. I changed the name as "path.php" it worked for me.. – Softcore Sep 23 '15 at 14:53