Apache子目录作为主服务器

时间:2015-01-14 22:47:32

标签: apache .htaccess virtualhost

我在api.host.com有一个VirtualServer,我需要在app.host.com/api访问相同的内容。

在Apache中,httpd.conf中有以下规则:

<VirtualHost app.host.com>
    DocumentRoot "C:\webserver\public\webapp"
    ServerName app.host.com
    <Directory "C:\webserver\public\webapp">
         Options Indexes FollowSymLinks Includes ExecCGI
         AllowOverride All
         Require all granted
    </Directory>    
    Alias /api/ "C:/webserver/public/api/"
    <Directory "C:/webserver/public/api/">
        Order allow,deny
        Allow from all
        Require all granted
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost>

C:/webserver/public/webapp中,.htaccess是:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

RewriteRule ^/api/(.*)$ /api/index.php?_url=/$1 [QSA,L]
RewriteRule ^api/(.*)$ api/index.php?_url=/$1 [QSA,L]

当我访问app.host.com/api/test时,我找不到404,但在app.host.com/api中,一切正常。

C:/webserver/public/api中,.htaccess是:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

api.host.com/test合作正常。

2 个答案:

答案 0 :(得分:1)

您需要交换规则的顺序。您的第一个规则^(.*)$正则表达式匹配您的所有请求,因此您需要在该规则有机会匹配请求之前使用您的API。像这样:

RewriteEngine On

RewriteRule ^/?api/(.*)$ /api/index.php?_url=/$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

答案 1 :(得分:0)

在httpd.conf中,我将路径更改为

Alias /**apicall**/ "C:/webserver/public/api/"
<Directory "C:/webserver/public/api/">
    Order allow,deny
    Allow from all
    Require all granted
    Options Indexes FollowSymLinks
</Directory>

并使用Jon Lin提示,将.htaccess规则更改为

RewriteRule ^api/?(.*)$  /apicall/index.php?_url=/$1 [QSA,L]