Problem redirecting 403 Forbidden to 404 Not Found

后端 未结 8 1367
心在旅途
心在旅途 2020-12-05 08:58

The pertinent part of my .htaccess looks like this:

Options -Indexes

    Order allow,deny
    Deny from all

Re         


        
相关标签:
8条回答
  • 2020-12-05 09:25

    Don't you want '^/include(/.*)?$'

    This part is a guess, but what would happen if you put the RedirectMatch above the block. That way you wouldn't by denying (forbidding) access to a request before you redirect that request to 404.

    0 讨论(0)
  • 2020-12-05 09:35

    With rewrite mod:

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} ^.*/\.
    RewriteRule ^(.*)$ - [R=404]
    

    Every file or dir who begin with a dot will be redirected to 404.

    /myDir/.svn => 404
    /.gitignore => 404
    /dir1/dir2_dir3/
    

    Or to change all 403,400 errors into 404 errors, put this at the end of /etc/apache2/conf.d/localized-error-pages OR into a .htaccess

    # Will raise a 404 error, because the file <fake_file_for_apache_404.php> doesn't exist.
    # We change 403 or 400 to 404 !
    ErrorDocument 400 /fake_file_for_apache_404.php
    ErrorDocument 403 /fake_file_for_apache_404.php
    # We need to rewrite 404 error, else we will have "fake_file_for_apache_404.php not found"
    ErrorDocument 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL <script type=\"text/javascript\">document.write(document.location.pathname);</script> was not found on this server.</p></body></html>"
    ErrorDocument 500 "Server in update. Please comme back later."
    
    0 讨论(0)
  • 2020-12-05 09:35

    This one works as expected for all files/dirs with name started by dot even in subdirectories:

    <FilesMatch "^\..+">
        Order allow,deny
        Deny from all
        Satisfy All
    </FilesMatch>
    RedirectMatch 404 \/\..+$
    
    0 讨论(0)
  • 2020-12-05 09:41
    ErrorDocument 400 /fake_file_for_apache_404.php
    ErrorDocument 403 /fake_file_for_apache_404.php
    ErrorDocument 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL <script type=\"text/javascript\">document.write(document.location.pathname);</script> was not found on this server.</p></body></html>"
    ErrorDocument 500 "Server in update. Please comme back later."
    

    I tried this solution, but I met another issue:

    Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request:

    0 讨论(0)
  • 2020-12-05 09:42

    A better and simple way is as follow :

    1 - Insert in your main conf (ie Outside VHOST world) :

    <IfModule mod_alias.c>
    RedirectMatch 404 ^/myDirectory(?|\/)$
    </IfModule>
    

    with myDirectory = css, js, images ...

    2 - You can set a per directory -Indexes as follow :

    Allow your "DirectoryIndex" Apache directive content to be served :

    <Directory "/myPathtoMyWebDocRoot/">
        Options Indexes
        AllowOverride None
        Order Allow, Deny
        Allow from all
    </Directory>
    

    and Deny directory index for others :

    <Directory "/myPathtoMyWebDocRoot/myDirectory/">
        Options -Indexes 
        AllowOverride None
        Order Allow, Deny
        Allow from all
    </Directory>
    

    with myDirectory = *, css, images, js, secret, ... following your needs.

    0 讨论(0)
  • 2020-12-05 09:47

    I can understand why the /include isn't caught by your RedirectMatch, you aren't making the end '/' optional, however the /include/config.inc part is a bit on the puzzling side.

    Here is what I got to work on Apache 2.2:

    <FilesMatch /include(/?|/.*)>
        Order allow,deny
        Deny from all
    </FilesMatch>
    
    RedirectMatch 404 ^/include(/?|/.*)$
    

    This handles these cases:

    /include 404
    /include/ 404
    /include/config.inc 404
    

    I had to change the FilesMatch part in order for the /include part to work properly.

    EDIT:

    The match line also works without the <FilesMatch> section in .htaccess and gives the expected results.

    0 讨论(0)
提交回复
热议问题