2014-01-13 102 views
0

我在www.domain.com上有主網站,並創建了一個名爲demo.domain.com的子域名爲 ,但是當我嘗試訪問子域名demo.domain.com時,它顯示與我的主域相同的網站,我該如何解決這個問題? 下面是我如何在我的Apache 2.2子域名顯示與主域名相同的網站

/etc/httpd/conf/httpd.conf中

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/domain.com/public_html 
    ServerName domain.com 
    ServerAlias www.domain.com 
</VirtualHost> 

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/demo.domain.com/public_html 
    ServerName demo.domain.com 
    ServerAlias www.demo.domain.com 
</VirtualHost> 

我甚至試過

service httpd restart 

它拋出

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName 
[Mon Jan 13 15:54:14 2014] [warn] _default_ VirtualHost overlap on port 80, the first has precedence 
定義的虛擬主機

我在主文件中添加了主域和子域

[[email protected]_html]# cat /etc/hosts 
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 
::1   localhost localhost.localdomain localhost6 localhost6.localdomain6 
1.2.3.4 domain.com demo.domain.com 

我在我的DNS管理器中爲子域名創建了一個A記錄,指向我的主vps IP地址。 我的DNS文件

demo IN A 1.2.3.4 
@   IN A 1.2.3.4 
www   IN A 1.2.3.4 

所以,當我訪問domain.com和demo.domain.com它顯示

我已經在

創建的index.html在同一個頁面domain.com的/var/www/demo.domain.com/public_html

[[email protected]_html]# cat index.html 
<html> 
    <head> 
    <title>www.example.com</title> 
    </head> 
    <body> 
    <h1>Success: You Have Set Up a Virtual Host</h1> 
    </body> 
</html> 

所以我是如何解決這一問題?我使用CentOS 6的,64位

當我嘗試訪問的子域demo.domain.com,其顯示的同一站點中我的主域名www.domain.com

+1

你在你的httpd.conf中有'NameVirtualHost *:80'的地方嗎?沒有這個,你將會打到默認的VirtualHost。 – arco444

+0

是的,我有#NameVirtualHost *:80 在我的httpd.conf中,但它的註釋掉了。 – AMB

+0

我剛剛評論它,並重新啓動apache和瞧,它的作品.... +1 – AMB

回答

0

正如arco444的建議。

嘗試

cat /etc/httpd/conf/httpd.conf | grep NameVirtualHost 

,並註釋掉

#NameVirtualHost *:80 

所以ITLL成爲

NameVirtualHost *:80 

然後重新啓動Apache通過輸入

service httpd restart 

現在它應該顯示子域的各自的網站/頁面。

0

當你說「它顯示與我的主域相同的網站「,它是否將子域顯示爲主域的子目錄?例如,www.domain.com/demo/。這對於一些控制面板和服務器設置來說是相當正常的,儘管不太理想。使用/htaccess代替使用httpd.conf,你可能會得到更好的結果:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com [NC] 
RewriteCond %{REQUEST_URI} ^/demo [NC] 
RewriteRule ^demo/?(.*)$ http://demo.domain.com/$1 [R=301,L] 
+0

nope,其顯示正確的子域http://demo.domain.com – AMB

+0

然後當你輸入演示時,它究竟顯示什麼。 domain.com? 「它顯示同一網站」是什麼意思?如果地址欄顯示demo.domain.com,有什麼問題? –