【问题标题】:Direct Apache requests under a certain URL directory to one file, and all others to a second file将某个 URL 目录下的 Apache 请求定向到一个文件,将所有其他请求定向到第二个文件
【发布时间】:2018-07-05 16:18:08
【问题描述】:

我正在尝试在我的 Raspberry Pi 3 上的 Apache 中同时托管一个 Angular 应用程序和一个极其简单的单文件 PHP CRUD API 后端。

我想配置规则重写(通过我已启用的 mod_rewrite)以首先将请求定向到已经存在的文件/目录。然后,我需要将 API 的请求(即~/api.php/{table}/{id} 形式的请求)定向到 PHP 文件。从那时起,对所有其他地方的所有请求都应定向到 Angular dist 的 index.html(我已经运行 ng build 并将生成的文件放到我的服务器上,这些该死的文件设置为“公共”)我在旋转我的轮子)

基本上,我需要做this,但两次:一次用于子目录,一次用于所有其他目录。

我对 Apache 配置文件非常陌生(让我们看看...... 2 小时后?),我确信这是一件相当简单的事情,但到目前为止,我对配置工具的了解有限阻止我提出解决方案。

任何帮助将不胜感激!

【问题讨论】:

    标签: php angular apache


    【解决方案1】:

    很久之后,我终于弄明白了。事实证明,我做错了几件事(看图)。

    我的第一个致命的谬误是我在.htaccess 文件中做生意,但我在这方面是错误的。我发现的第一个问题是.htaccess 文件是 并且应该避免,如果您可以将全局配置与<Directory "/var/www/html/..."></Directory> 语句结合使用,则应该完全忽略它。我犯的第二个错误(注意:我在发布上述问题后犯了这个错误)是 .htaccess 规则不适用于我假设的层次结构;恰恰相反,事实上。 .Htaccess 下级子目录中的配置文件实际上被其上级目录 kin 覆盖,而不是相反。

    我的第二个错误实际上是我使用 RewriteRule 语句的方式。对于其他只是想与 Apache 相处,并且对学习其内部机制不感兴趣的人,请记住,通过阅读 documentation,您实际上可能会节省更多的时间。实际上,这对大多数事情都是如此。

    可以在下面找到最终成功的代码。 (这是来自 /etc/apache2/sites-enabled/000-default.conf 文件,通过 SSH 连接通过 sudo nano 编辑)

    <VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com
    
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
    <Directory "/var/www/html">
        AllowOverride All
    </Directory>
    <Directory "/var/www/html/">
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !/api
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.html [L]
    </Directory>
    <Directory "/var/www/html/api/">
        RewriteBase /api/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /api/api.php/$1 [L]
    </Directory>
    
    </VirtualHost>
    # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
    

    这会将所有不以/api/(例如/home/faq)开头的请求重定向到Angular 的index.html,并将任何API 请求(api/People/27/api/Customers)重定向到api.php 文件(位于/api/api.php)。

    编辑:我偶然发现了上述代码的一个问题,即它没有将实际 URL 转发到 api.php 以使其发挥作用。这已通过匹配^(.*)$ 并使用$1 将答案与重定向规则联系起来得到纠正

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 2020-11-06
      • 2013-06-24
      • 2014-12-05
      • 2022-08-07
      相关资源
      最近更新 更多