WAMP - Remove localhost from project URL
When configuring a local development stack like WAMP, the default behavior often leads to URLs that include localhost or 127.0.0.1. For instance, if your project resides in c:\wamp\www\project1, accessing it via your browser will yield http://localhost/project1/ rather than the cleaner http://project1/. While this works for basic HTML rendering, it introduces significant friction when interacting with server-side frameworks. The primary culprit is how PHP handles the $_SERVER['HTTP_HOST'] variable and virtual hosting directives.
The core issue lies in the Apache configuration file located at C:\wamp\bin\apache\Apache24.x\conf\extra\httpd-vhosts.conf. By default, WAMP installs a single <VirtualHost> block that listens on port 80 for all requests. Because there is no specific directive mapping the domain name to the folder path, Apache defaults to serving content from the htdocs directory relative to the root of the localhost server, effectively prefixing every request with localhost.
To resolve this and achieve a custom domain-like URL without registering an actual domain name, you need to configure a virtual host. This process involves editing the configuration file mentioned above. First, ensure your project folder is correctly placed in c:\wamp\www\project1. Then, add a new <VirtualHost *:80> block that explicitly maps the desired hostname to the local directory.
Here is an example of what the configuration should look like:
<VirtualHost *:80>
ServerName project1
DocumentRoot "C:/wamp/www/project1"
<Directory "C:/wamp/www/project1">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
In this setup, the ServerName directive tells Apache what hostname to expect. When you request http://project1/, Apache checks its virtual host list, sees that project1 matches the ServerName, and then serves files from the specified DocumentRoot. This bypasses the default behavior where Apache assumes a generic root without a specific server name.
It is crucial to note that this configuration only works if you are accessing the site via the custom hostname (http://project1/). If you try to access it via http://localhost/project1/, Apache will still serve the content, but your application logic might behave differently depending on how it resolves URLs. Many frameworks, including Laravel and Symfony, use the SERVER_NAME variable to determine the base URL for asset generation and redirects. If this variable returns localhost instead of project1, your application might generate broken links or fail to access certain server variables correctly.
After making changes to the Apache configuration, you must restart the WAMP services for the settings to take effect. Right-click on the WAMP icon in the system tray and select "Restart All Services." This ensures that Apache re-reads the httpd-vhosts.conf file with your new virtual host definitions.
One potential pitfall is the .htaccess file within your project folder. If you have a custom .htaccess rule attempting to rewrite localhost/* to the project root, this will conflict with the virtual host configuration. Once the virtual host is set up correctly, remove any conflicting rewrites that force localhost usage.
If you are using PHP-FPM instead of the built-in Apache module, ensure that your php.ini or framework-specific configuration does not hardcode the base URL to localhost. The goal is to make your local environment indistinguishable from a production server regarding hostname resolution. This approach allows you to develop against a clean URL structure without needing to purchase a domain name or configure DNS records. For teams managing multiple projects, this method scales easily; simply duplicate the virtual host block and change the ServerName and DocumentRoot paths accordingly.