在 httpd.conf 中设置适用于所有服务站点的重写规则

Set a rewrite rule in httpd.conf which applies for all served sites

好的,所以我有一个服务于多个网站的服务器。

www.site1.com
www.site2.com
...

我想在 httpd.conf(或我将包含在 httpd.conf 中的另一个文件)中设置我的 rewriterule,以便它同时适用于所有网站,而不必为每个站点单独。

这行不通:

<Directory "${INSTALL_DIR}/www/">
    Options Includes FollowSymLinks
    AllowOverride none
    Require all granted
    RewriteRule ^/news/article/(.*)/(.*)$ news/article.php?id=
</Directory>

这个有效:

<Directory "${INSTALL_DIR}/www/site1/">
    RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=
</Directory>
<Directory "${INSTALL_DIR}/www/site2/">
    RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=
</Directory>

所以基本上我可能必须添加一些 Rewriterule [这里的东西]/news/article/(.)/(. )$ news/article.php?id=$1 因此它适用于所有网站的 www-dir。

可能是我遗漏了一些愚蠢的东西...我已经做了一些实验,但我无法让它工作。非常感谢任何帮助!

编辑:这是供将来参考的解决方案。 在虚拟主机文件中:

<Virtualhost site1>
...
        <Directory "${INSTALL_DIR}/www/site1">
            Include "${APACHE_DIR}/conf/extra/siterules.conf"
        </Directory>
...
</Virtualhost>
<Virtualhost site2>
...
        <Directory "${INSTALL_DIR}/www/site2">
            Include "${APACHE_DIR}/conf/extra/siterules.conf"
        </Directory>
...
</Virtualhost>

在siterules.conf中:

RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=
and some other rules if you want.

仅此而已 :-) 您只需在一个地方更改规则,它就适用于所有网站。

非常感谢@arkascha 为我指明了正确的方向。

通常使用单独的虚拟主机实现单独的站点。如果是这样,那么我通常使用包含共享规则或指令的配置文件,我将其包含在所有虚拟主机定义中。这允许在将规则应用于包括虚拟主机在内的所有位置时在单个位置调整规则。

如果您不对不同的位置(注意:位置,而不是主机...)操作不同的规则,那么事情就更简单了:您可以简单地在目录块之外实施规则。