【问题标题】:Apache subdirectory as main serverApache 子目录作为主服务器
【发布时间】:2015-03-13 06:46:05
【问题描述】:

我在api.host.com 有一个虚拟服务器,我需要在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 配合使用也很好。

【问题讨论】:

    标签: apache .htaccess virtualhost


    【解决方案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]
    

    【讨论】:

      【解决方案2】:

      在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]
      

      【讨论】:

        猜你喜欢
        • 2013-01-29
        • 2013-03-20
        • 1970-01-01
        • 1970-01-01
        • 2014-03-08
        • 2019-05-07
        • 2018-05-08
        • 1970-01-01
        相关资源
        最近更新 更多