首页 > 解决方案 > 在 httpd.conf 中设置适用于所有服务站点的重写规则

问题描述

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

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

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

这不起作用:

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

这有效:

<Directory "${INSTALL_DIR}/www/site1/">
    RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=$1
</Directory>
<Directory "${INSTALL_DIR}/www/site2/">
    RewriteRule ^news/article/(.*)/(.*)$ #news/artile.php?id=$1
</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=$1
and some other rules if you want.

就是这样 :-) 您在一处更改规则,它适用于所有站点。

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

标签: apache.htaccessmod-rewriteurl-rewritinghttpd.conf

解决方案


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

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


推荐阅读