Rewrite file extension BUT deny direct access to file

后端 未结 1 757
梦如初夏
梦如初夏 2021-01-03 05:03

I am looking for a way to hide the file extension via .htaccess and deny direct access. Let\'s consider the following:

http://www.xyz.zyx/index.php 
<         


        
相关标签:
1条回答
  • 2021-01-03 05:23

    Although one may question the usefulness of such a thing, it's feasible, so here's how to do it in a .htaccess using mod_rewrite:

    RewriteEngine On
    
    # Sets your index script
    RewriteRule ^$ index.php [L]
    
    # Condition prevents redirect loops (when script is not found)
    RewriteCond %{ENV:REDIRECT_STATUS} !^$
    RewriteCond %{REQUEST_FILENAME} !-f
    
    # Stop here if the file is not found after a redirect
    RewriteRule ^(.*)$ notfound.php [L]
    
    # Condition prevents redirect loops (when script is found)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    
    # Forbid access directly to PHP files
    RewriteRule ^.*?\.php$ forbidden [F,L]
    
    # Make sure the filename does not actually exist (images, etc.)
    RewriteCond %{REQUEST_FILENAME} !-f
    
    # Append the .php extension to the URI
    RewriteRule ^(.*)$ $1.php [L] 
    
    0 讨论(0)
提交回复
热议问题