Rewrite requests to directory with htaccess to 404 error

白昼怎懂夜的黑 提交于 2020-04-18 00:49:10

问题


I would like to rewrite requests to directory secret (http://example.com/secret) with .htaccess file to show always 404 Not Found error. I'm using Apache 2.4.

It should work for those cases:

  • /secret/treasure.php (all files and directories in that folder)
  • /secret
  • /secret/
  • /secret?42
  • /secret/?23
  • /secret/.php should result in 404 Not Found
  • /secret/.htaccess should result in 404 Not Found

My .htaccess rules block everything except the last two cases (I'm getting 403 error, but it should be 404):

Options -Indexes
RewriteEngine On
RewriteRule ^secret.*$ - [R=404,L]

I would like to rewrite requests /secret/.php and /secret/.htaccess to "404 Not Found error".


UPDATE: This is a bit hacky but seems to block everything

Options -Indexes
RewriteEngine On
RewriteRule ^secret.*$ - [R=404,L]

RewriteRule ^404_error$ - [R=404,L]

<FilesMatch "^\.">
    order allow,deny

    # 403 error
    deny from all

    # Rewrite 403 error to 404 error
    # Must be an absolute path
    ErrorDocument 403 /404_error
</FilesMatch>

来源:https://stackoverflow.com/questions/61198514/rewrite-requests-to-directory-with-htaccess-to-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!