我有一個博客,位於mysite.tld/blog
.目前透過根目錄mysite.tld
重定向到。顯然,當我兩年前設置它時,我無法讓根目錄中的文件正常工作。在檔案系統上,我的部落格位於.mysite.tld/blog
index.php
.httaccess
/var/www/blog
現在我建立一個簡單的網站,我希望將其顯示在mysite.tld
。它透過 PHP 微框架工作,該框架透過index.php
文件提供頁面服務。我在 git 存儲庫中有這個網站,在這個存儲庫中,它index.php
位於www/index.php
.我想在我的伺服器上有一個儲存庫的克隆,這樣我就可以更新到新版本。因此,假設我將其克隆為site
,/var/www
入口點最終為/var/www/site/www/index.php
。
在過去的 3 個小時裡,我一直試圖讓這個極其簡單的事情發揮作用,但沒有成功。我想要做的就是將 URL 映射/blog
到/var/www/blog
並將 URL 的其餘部分映射/
到/var/www/site/index
.為此我需要什麼神奇的配置?
這是輸出lsb_release -a
:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.2 LTS
Release: 14.04
Codename: trusty
這是輸出apache2ctl -V
:
Server version: Apache/2.4.7 (Ubuntu)
Server built: Jul 22 2014 14:36:38
Server's Module Magic Number: 20120211:27
Server loaded: APR 1.5.1-dev, APR-UTIL 1.5.3
Compiled using: APR 1.5.1-dev, APR-UTIL 1.5.3
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"
這是輸出apache2ctl -M
:
Loaded Modules:
core_module (static)
so_module (static)
watchdog_module (static)
http_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
unixd_module (static)
access_compat_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
filter_module (shared)
mime_module (shared)
mpm_prefork_module (shared)
negotiation_module (shared)
php5_module (shared)
reqtimeout_module (shared)
rewrite_module (shared)
setenvif_module (shared)
status_module (shared)
答案1
我想要做的就是將 URL 映射
/blog
到/var/www/blog
並將 URL 的其餘部分映射/
到/var/www/site/index
.為此我需要什麼神奇的配置?
你可以輕鬆地做到這一點阿帕契的Alias
指令像這樣。請注意,我發現您使用的是 Apache 2.4.7,它的語法可能與本建議所基於的 Apache 2.2 及更早版本的語法略有不同。儘管如此,雖然語法可能略有不同,但總體概念仍然相同,而且我相當有信心 Apache 2.4 仍然具有Alias
功能。
這些項目將在主站點的 Apache 設定檔中設置,該檔案位於/etc/apache2/sites-available/
.現在它們可能位於名為的檔案中/etc/apache2/sites-available/default
或位於主機名稱的單獨檔案中,/etc/apache2/sites-available/mysite.tld
因此請務必在徹底修改檔案之前檢查您的配置。
這會將所有請求設定為http://mysite.tld/blog
從以下位置取得內容/var/www/blog
:
Alias /blog /var/www/blog
這會將所有請求設定為http://mysite.tld/
從以下位置取得內容/var/www/site/index
:
Alias / /var/www/site/index
話雖如此,您可能只需要Alias /blog /var/www/blog
;只要您設定為 ,Alias / /var/www/site/index
則可能不需要這一秒鐘。DocumentRoot
/var/www/site/index
關於 Apache 設定如何運作的良好、簡單和簡潔的概述可以在這個網站上找到。