Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache - Domain for localhost to access folders as http://folder.local

I'm running XAMPP on Ubuntu and I'd like to create a virtual host for my projects, so that I have a tld assigned to my server root directory (for example .local) and folders inside it accessible through URLs as http://foldername.local.

Also, how much more complicated would it be to use .htaccess to have http://someotherdomain.local redirect to the /foldername path in the server root?

like image 329
Томица Кораћ Avatar asked Feb 19 '23 02:02

Томица Кораћ


1 Answers

I've managed to do it on my own. It is possible to do it, however you'll need to install a DNS server.

Note: I decided to use .dev as my local domain, so in the following examples, the dev part will refer to my chosen domain. Keep that in mind.

Install and configure DNS Server

It shouldn't matter which one it is, but you'll need to know how to configure it properly. The configuration depends on which DNS server you chose. I went for dnsmasq. It's lightweight and very handy.

An important note for Ubuntu users is that since Ubuntu 11.10 there is already a light version called dnsmasq-base installed, which will cause conflicts during installation. I won't be explaining here how to get around this, because there are many instructions available elsewhere.

Once you have your DNS server installed, you should configure it to listen for the address equal to your desired domain.

In my case with dnsmasq, that meant opening /etc/dnsmasq.conf and changing line #62 to this: address=/dev/127.0.1.1

Configure Web server

Assuming that you already have some kind of Server software installed, you need to make a few tweaks.

First, you should edit your hosts file to map your desired domain to your localhost.

in my case of XAMPP for Linux on Ubuntu, this means I opened /etc/hosts and changed lines

127.0.0.1 localhost
127.0.1.1 tomica-ubuntu

to

127.0.0.1 localhost
127.0.1.1 tomica-ubuntu dev

This will redirect http://dev to my local server.

Next, create a new virtual host with a couple of specific options, like this:

In my case, that means opening /opt/lampp/etc/extra/httpd-vhosts.conf and adding this at the end of the file:

<VirtualHost *:80>
    DocumentRoot "/opt/lampp/htdocs/dev"
    ServerName dev
    ServerAlias *.dev

    <Directory /opt/lampp/htdocs/dev>
        AllowOverride All
    </Directory>
</VirtualHost>

For the sake of brevity, I won't explain this piece of code, since documentation is also available.

After all this is done, start your DNS and Web servers, or restart them if they're already running.

Configure .htaccess

Open root folder of your newly created host. That's the folder devined in your . In my case, that's /opt/lampp/htdocs/dev. In there, create a .htaccess file and put this in it:

# Specify order of index files; if none exist, show files list
  DirectoryIndex index.php index.html

# Interpret .html files as .php scripts
  AddHandler php5-script .php .html

# THE MAGIC - Redirect subdomains of .dev to their respective folders
  RewriteEngine on
  Options +FollowSymlinks
  RewriteBase /

  RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
  RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.dev(.*)?$ [NC]
  RewriteRule !^%2\.dev%3?/$ http://dev/%2%{REQUEST_URI}/ [P]

Again, explaining all this would require too much space and time. Just copy/paste and don't worry :) But don't forget to change my dev to anything you chose for your domain name.

AND THAT'S IT! By now you should be able to browse your project using addresses like http://folder.dev/, http://www.folder.dev, http://folder.dev/file.html, http://folder.dev/subfolder/document.txt etc.

As a bonus, I will add just one more advice. The reason why I did all this is so that I could more easily develop my Laravel and WordPress prjects. However, with Laravel, you should redirect the url http://lvproject.dev/ to the location of /lvproject/public. And here is the .htaccess file that enables just that. Open your /lvproject folder, create a .htaccess file and place this code in it:

RewriteBase /lvproject/

RewriteCond %{REQUEST_URI} lvproject/index\.php [NC]
RewriteRule index\.php(.*)$ public/ [L]

Two drawbacks of this solution are: 1) RewriteBase rule needs to be set anew for every new project (i.e. you need to manually create .htaccess in each new project); 2) Your project will be available from both http://lvproject.dev/ and http://lvproject.dev/public/, which is not cool, but I'm too lazy at the moment to get it fixed :)

like image 165
Томица Кораћ Avatar answered Apr 25 '23 06:04

Томица Кораћ