【问题标题】:Block access to all .php files except some specific files (htaccess)Block access to all .php files except some specific files (htaccess)
【发布时间】:2022-12-02 01:27:43
【问题描述】:

I want to send visitor to custom 404 error page when it tries to access to a .php URL, but with some specific exceptions:

Exceptions like:

(maybe-something/)/allowed-file.php

My current .htaccess file blocks access to all .php files:

ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -MultiViews

RewriteEngine on
RewriteCond %{SERVER_PORT} 80

# Force WWW & SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

##.php envia a 404
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]

How can I add a few dozen of specific exceptions?

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:
    ##.php envia a 404
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.+).php - [F,L]
    

    Change this to:

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_URI} !/allowed-file.php$
    RewriteRule ..php - [F]
    

    The ! prefix negates the expression. So the 2nd condition is successful when the requested URL isnot/allowed-file.php or /<anything>/allowed-file.php. The 2nd argument to the RewriteCond directive (following the ! prefix) is a regular expression (by default).

    To add more exceptions; add moreconditions(RewriteCond directives).

    Note that I removed the capturing group in the RewriteRulepatternsince this is not being used. And the L flag is not required when using the F flag (it is implied).

    However, this rule is in the wrong place in your config file. It should be immediately after the RewriteEngine directive andbeforethe canonical redirects.

    Also, that stray RewriteCond %{SERVER_PORT} 80 directive (immediately after the RewriteEngine directive) is superfluous and should be removed (to avoid accidental errors later).

    Reference:

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 2022-08-27
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2022-12-27
      • 2022-12-28
      • 2022-05-19
      • 2013-03-01
      相关资源
      最近更新 更多