将对 .htaccess 的访问重定向到另一个文件
Redirect access to .htaccess to another file
我一直在研究 .htaccess 文件,完全出于好奇,我想知道是否可以将对 .htaccess 文件本身的访问重定向到另一个文件?
示例:
用户前往 http://www.example.com/.htaccess
我希望用户被重定向到 index.php?foo=bar
我试过以下方法:
Redirect 301 /.htaccess /index.php?foo=bar
和
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^((?s).*)$ index.php?foo=bar [QSA,L]
</IfModule>
这些方法适用于任何其他文件,但 .htaccess
文件本身总是会导致显示 403 Forbidden
页面。
这可能吗?如果不是,那为什么?
如果您查看网络服务器配置文件,您会发现一些指令阻止用户从浏览器访问 .htaccess
或 .htpasswd
文件。例如,如果我打开位于 /etc/apache2/apache.conf
的 apache 配置,我可以发现以下几行:
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
所以您应该可以将上面的内容更改为:
Order allow,deny
Allow from all
您还需要确保您对 .htaccess
文件具有正确的文件权限,最后使用以下命令重新启动服务器:
sudo service apache2 restart
当然,按照上面的方法做起来不太安全。
我一直在研究 .htaccess 文件,完全出于好奇,我想知道是否可以将对 .htaccess 文件本身的访问重定向到另一个文件?
示例:
用户前往 http://www.example.com/.htaccess
我希望用户被重定向到 index.php?foo=bar
我试过以下方法:
Redirect 301 /.htaccess /index.php?foo=bar
和
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^((?s).*)$ index.php?foo=bar [QSA,L]
</IfModule>
这些方法适用于任何其他文件,但 .htaccess
文件本身总是会导致显示 403 Forbidden
页面。
这可能吗?如果不是,那为什么?
如果您查看网络服务器配置文件,您会发现一些指令阻止用户从浏览器访问 .htaccess
或 .htpasswd
文件。例如,如果我打开位于 /etc/apache2/apache.conf
的 apache 配置,我可以发现以下几行:
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
所以您应该可以将上面的内容更改为:
Order allow,deny
Allow from all
您还需要确保您对 .htaccess
文件具有正确的文件权限,最后使用以下命令重新启动服务器:
sudo service apache2 restart
当然,按照上面的方法做起来不太安全。