apache - 在 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 为我指明了正确的方向。
解决方案
通常使用单独的虚拟主机来实现单独的站点。如果是这样,那么我通常使用包含共享规则或指令的配置文件,这些规则或指令包含在所有虚拟主机定义中。这允许在单个位置调整规则,同时将它们应用于包括虚拟主机在内的所有主机。
如果您不对不同的位置操作不同的规则(注意:位置,而不是主机...),那么事情就更简单了:您可以简单地在目录块之外实现规则。
推荐阅读
- scala - 如何在 spark 驱动程序中访问通过 --files 提交的文件?
- angular - 发生未处理的异常:找不到模块“restore-cursor”
- python - 为什么RegularGridInterpolator 比NearestNeighbourInterpolator 使用更多的内存?
- sql - 如何在不使用表的情况下交叉连接?
- pyspark - 如何在pyspark中将多列合并为一列
- angular - 在Angular 12中手动输入日期时,Angular Date Picker将无效的日期格式转换为有效的日期
- javascript - 如何在不覆盖我的 CSS 文件的情况下将本地 Bootstrap CSS 导入 React 应用程序?
- python - pandas loc 按日内数据的日期
- symfony - Symfony 表单提交错误“复合表单在提交时需要一个数组或 NULL”,用于禁用的表单字段
- sql - 在 SSIS 中删除文本末尾的 CLRF