Apache - 本地主机域以 http://folder.local 访问文件夹 [英] Apache - Domain for localhost to access folders as http://folder.local

查看:15
本文介绍了Apache - 本地主机域以 http://folder.local 访问文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ubuntu 上运行 XAMPP,我想为我的项目创建一个虚拟主机,这样我就有一个 tld 分配给我的服务器根目录(例如 .local),其中的文件夹可以通过 URL 访问http://foldername.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.

另外,使用 .htaccess 将 http://someotherdomain.local 重定向到服务器根目录中的 /foldername 路径会复杂多少?

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?

推荐答案

我已经设法自己完成了.可以这样做,但是您需要安装 DNS 服务器.

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

注意:我决定使用 .dev 作为我的本地域,所以在下面例如, dev 部分将引用我选择的域.保持在介意.

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.

安装和配置 DNS 服务器

不管它是哪一个,但您需要知道如何正确配置它.配置取决于您选择的 DNS 服务器.我选择了 dnsmasq.它重量轻,非常方便.

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.

Ubuntu 用户需要注意的是,自 Ubuntu 11.10 以来已经安装了一个名为 dnsmasq-base 的轻量级版本,它将在安装过程中引起冲突.我不会在这里解释如何绕过这个,因为其他地方有很多说明.

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.

安装好 DNS 服务器后,您应该将其配置为侦听与所需域相同的地址.

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

在我使用 dnsmasq 的情况下,这意味着打开 /etc/dnsmasq.conf 和将第 62 行更改为:address=/dev/127.0.1.1

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.

首先,您应该编辑您的 hosts 文件以将所需的域映射到您的本地主机.

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

在我的 XAMPP for Linux on Ubuntu 中,这意味着我打开了/etc/hosts 和更改的行

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

127.0.0.1 localhost
127.0.1.1 tomica-ubuntu dev

这会将 http://dev 重定向到我的本地服务器.

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

接下来,使用几个特定选项创建一个新的虚拟主机,如下所示:

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

就我而言,这意味着打开/opt/lampp/etc/extra/httpd-vhosts.conf 并在末尾添加文件:

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.

所有这些都完成后,启动您的 DNS 和 Web 服务器,如果它们已经在运行,则重新启动它们.

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

打开新创建主机的根文件夹.那是您的 .在我的例子中,那是 /opt/lampp/htdocs/dev.在那里,创建一个 .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]

同样,解释所有这些需要太多的空间和时间.只需复制/粘贴,别担心 :) 但不要忘记将我的 dev 更改为您为域名选择的任何内容.

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.

就是这样! 现在您应该可以使用 http://folder.dev/ 等地址浏览您的项目,http://www.folder.devhttp://folder.dev/file.htmlhttp://folder.dev/subfolder/document.txt

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.

作为奖励,我将再添加一条建议.我这样做的原因是我可以更轻松地开发我的 Laravel 和 WordPress 项目.但是,使用 Laravel,您应该将 url http://lvproject.dev/ 重定向到 /lvproject/public 的位置.这是启用此功能的 .htaccess 文件.打开您的 /lvproject 文件夹,创建一个 .htaccess 文件并将此代码放入其中:

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]

这个方案的两个缺点是:1)RewriteBase规则需要为每个新项目重新设置(即你需要在每个新项目中手动创建.htaccess);2) 您的项目将可从 http://lvproject.dev/http://lvproject.dev/public/ 获得,这并不酷,但我'我现在懒得修复它:)

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 :)

这篇关于Apache - 本地主机域以 http://folder.local 访问文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆