WAMP - Remove localhost from project URL

In your www folder open index.php at line 30. Here, change $suppress_localhost to be false. So, it is should look:

$suppress_localhost = false;

That was the quickest and easiest fix for me. I'm using 64 bit Wamp.


Okay, I had this problem. So, I troubleshooted the problem and traced it to an actual solution, NOT A HACK.

The SOLUTION :

  1. Right-Click WAMP-server icon
  2. Select WAMP Settings,
  3. Select (enable) option Add localhost in URL


DONE. The remainder is FYI of how and why.


Note: localhost/myproject.php or myproject.php. Although the solution was already accepted, I saw some posts that got me confused. The accepted solution is based on a single project wrt different server applications, based on the OP's specific question, and how to influence that single project in question. But all the other solutions are hacks and don't really answer the solution to the OP's question, but do bring up a good point about the URL. So, according to the other "solutions", here is how to toggle the localhost reference in the URL. Hence my additional solution added to the mix.


This is a toggle switch.

Troubleshooting Process (no hacking involved):



Let's peek at the index.php

enter image description here


Let's look at the config file. Note the variables and Array?

enter image description here


Here is the array. A variable used earlier. Let's see... Oh, it tells us where and what to do.

enter image description here


As noted in the SOLUTION:

enter image description here


in your www folder open index.php at line 30 change: $suppress_localhost to be false

this is should look:

$suppress_localhost = false;


You can also look at this answer specially if you are now using WAMPServer 3 or greater, for a simple clikc and go way to create Virtual hosts.

Actually this change was intended by the WAMPServer developers and for a good reason.

There is a problem using the localhost/project1 url and the default WAMPServer DocumentRoot in that it causes problems for some frameworks and WordPress type environments, as well as your own code if you are using code which depends on knowing anything about the server environment.

The correct solution is to create Virtual Hosts for all your projects even those that you store in the \wamp\www\project1 style folders.

When doing that the DocumentRoot is \wamp\www and that is what causes these problems.

These tools expect the DocumentRoot to be the root of the site i.e. \wamp\www\project1 so that when they use PHP variables like

$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']

they get the correct answer i.e. the answer they would get on a real live server hosting just that site.

So using the localhost\project1 style url would mean these variables would return

$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www

When they should return

$_SERVER['HTTP_HOST'] = project1
$_SERVER['SERVER_NAME'] = project1
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www/project1

So what you should do to make the My Projects menu work and reduce your pain in copying sites to live servers is:

Create an entry in the HOSTS file for each project like so and remember to create one for access via IPV4(127.0.0.1) and one for access via IPV6 (::1):-

127.0.0.1 localhost
127.0.0.1 project1

::1 localhost
::1 project1

Remember to refresh the Windows DNS Cache after any change to this file like so :-

Start a command window using Run as Administrator and run :-

net stop Dnscache
net start Dnscache

Now you must create a Virtual Host definition, so edit the \wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf file ( apache versions may differ )

Delete the default stuff in there the first time you do this. And then create your Virtual Host definitions like so :-

#
# Use name-based virtual hosting.
# This next line is not required if you are using Apache 2.4.x and should be deleted
NameVirtualHost *:80

## should be first so the wamp menu page loads and is the default site
## should also never be changed from only allowing access from the local machine
## for a bit of extra security from casual ip address probing
<VirtualHost *:80>
    DocumentRoot "C:/wamp/www"
    ServerName  localhost
    ServerAlias localhost
    <Directory  "C:/wamp/www">
        AllowOverride All

        <IfDefine APACHE24>
            Require local
        </IfDefine>

        <IfDefine !APACHE24>
            Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1 localhost ::1
        </IfDefine>
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/project1"
    ServerName  project1
    ServerAlias project1
    <Directory  "C:/wamp/www/project1">
        AllowOverride All

        <IfDefine APACHE24>
            Require local
        </IfDefine>

        <IfDefine !APACHE24>
            Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1 localhost ::1
        </IfDefine>

    </Directory>
</VirtualHost>

Now you need one more change, you must uncomment the line in httpd.conf that includes the above file we have just changed. So edit the httpd.conf file, use the wampmanager menus to do this as it ensures you edit the correct file.

Find this line #Include conf/extra/httpd-vhosts.conf and remove the comment # symbol from the beginning of the line like so :-

Include conf/extra/httpd-vhosts.conf

Now of course you will need to restart Apache so that it picks up your configuration changes.

If Apache does not restart, you probably made a mistake in the config, to find out what is wrong try this.

Open a command window and CD into the \wamp\bin\apache\apache2.4.9\bin folder.

Then run this :-

httpd -t

If the error is in httpd.conf or the httpd-vhost.conf files it will tell you the error and also give you the line number to make finding the error very easy.