Project Links Do Not Work on Wamp Server

WAMP Server Your Projects urls are invalid & do n't work

Your HOSTS file is basically empty as a # is a comment.

You have to add a reference to each VHOST into your HOSTS file like this so that the browser can find your development domains

Hosts file

127.0.0.1 localhost
::1 localhost
127.0.0.1 phptest
::1 phptest

Now you must restart the DNS Cache like this, from a command prompt that you have started "As an Administrator" or just reboot the PC

>net stop dnscache

When that has completed do

>net start dnscache

Or there is a menu item on WAMPServers menus

[Right Click] wampmanager-> Tools -> Restart DNS

WAMP Server can not direct to the home page

Perhaps I'm oversimplifying, but the root is just localhost and a link specified as href="/" will always take you there. To link to the localhost/test directory, which is not your root directory it's href="/test"

wampserver -- Links are broken on localhost

By removing the / you changed it to relative paths.

So you must now think about your path structure.

For example, if the project was placed in a folder, so the root is: http://localhost/mycms/ then you go to ./mycms/cats the relative path to your CSS would be translated to: ./mycms/cats/css/styles.css which would break it.

You should look into using absolute paths to assets. For example:

<link rel="stylesheet" href="//localhost/mycms/css/styles.css"/>

This can be achieved by defining or detecting the current URL path from $_SERVER variables and prepending your hostname.

So something like which stores in a constant which you can then use throughout your code which will result in the correct absolute path to the webroot:

<?php
define('SITE_URL', rtrim(sprintf(
'%s://%s/%s',
isset($_SERVER['HTTPS']) || $_SERVER["SERVER_PORT"] == "443" ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
trim(dirname($_SERVER['SCRIPT_NAME']), '/')
), '/'));

echo SITE_URL;

Then just echo that in your templates.

<link rel="stylesheet" href="<?= SITE_URL ?>/css/styles.css"/>


Related Topics



Leave a reply



Submit