首页 > 解决方案 > 用于无条件附加“index.php”的 Apache2 RewriteRule

问题描述

我在 Debian 10 上使用 Apache 2.4.38 和 PHP v7.3 运行网站“example.com”。

在主 apache2 配置文件 /etc/apache2/apache2.conf 我有一个 RewriteRule 自动将 http 转换为 https。这条规则完美运行。摘抄:

<IfModule mod_rewrite.c> 
  RewriteCond    %{HTTPS}  !=on 
  RewriteRule    ^/?(.*)   https://%{SERVER_NAME}/$1  [R=301,L] 
</IfModule>

对于特定的虚拟主机文件(内容就在下面)

<VirtualHost *:443> 
  <Directory /var/www/html/abc> 
   <IfModule dir_module>  
     DirectoryIndex    index.php 
   </IfModule>  
   Options          FollowSymLinks Indexes MultiViews 
   AllowOverride    None 
   Require          all granted         
  <IfModule mod_rewrite.c> 
   RewriteCond  %{REQUEST_URI} !^(/index\.php|/img|/js|/css|robots\.txt|/favicon\.ico) 
     RewriteRule  ^(.*)$  %{HTTP_HOST}/$1/index.php  [R=301,L] 
  </IfModule> 
 </Directory> 
</VirtualHost>

调用任何一个

应该导致https://example.com/abc/index.php(即字符串“index.php”应该无条件地附加到完全限定的字符串“https://example.com/abc”</p>

不幸的是我没有成功。有人可以指出我正确的解决方案吗?

您是否建议将第二个 RewriteRule(我正在寻找工作)也放入文件 /etc/apache2/apache2.conf 中,还是应该将其放入 /var/www.html/abc 的虚拟主机文件中?

请注意:我不希望将第二个 RewriteRule 放入 .htaccess 文件中。

标签: apache

解决方案


推荐阅读