要将子目录中的直接URL重定向到其父目录,可以在Apache2配置文件中使用重定向规则。以下是一个示例:
打开Apache2的配置文件,通常位于 /etc/apache2/apache2.conf
或 /etc/apache2/sites-available/000-default.conf
。
在配置文件的合适位置,添加以下代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^subdirectory/(.*)$ /$1 [L,R=301]
替换 /path/to/parent/directory
为父目录的实际路径。
保存配置文件并重新启动Apache2服务器。
现在,当访问 http://example.com/subdirectory/example.html
时,将自动重定向到 http://example.com/example.html
。