如何在服务器中授予访问权限执行特定的php文件?

时间:2016-12-18 18:44:47

标签: php apache .htaccess mod-rewrite octobercms

大家好,首先抱歉我的英语不好!我在服务器中运行特定的php文件时遇到问题,我想配置.htaccess以允许用户运行该文件。 例如:www.mywebsite.com/theme/test.php

这是我的.htaccess文件,当我查看我的网站时,它返回" 404 Not Found" `

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine on

##
## You may need to uncomment the following line for some hosting environments,
## if you have installed to a subdirectory, enter the name here also.
##
# RewriteBase /

##
## Black listed folders
##
RewriteRule ^bootstrap/.* index.php [L,NC]
RewriteRule ^config/.* index.php [L,NC]
RewriteRule ^vendor/.* index.php [L,NC]
RewriteRule ^storage/cms/.* index.php [L,NC]
RewriteRule ^storage/logs/.* index.php [L,NC]
RewriteRule ^storage/framework/.* index.php [L,NC]
RewriteRule ^storage/temp/protected/.* index.php [L,NC]
RewriteRule ^storage/app/uploads/protected/.* index.php [L,NC]

##
## White listed folders
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/.well-known/*
RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/.*
RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
RewriteRule !^index.php index.php [L,NC]


##
## Block all PHP files, except index
##
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{REQUEST_FILENAME} \.php$
 RewriteRule !^index.php index.php  [L,NC]

##
## Standard routes
##

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^ index.php [L]

` 有什么方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

您的问题是标有&#34;阻止所有PHP文件的规则,除了索引&#34;将所有PHP文件请求路由到索引文件。以下是解决此问题的两种方法。

简单的一次性解决方案

您只需在任何其他RewriteRule行之前添加此规则:

RewriteRule ^theme/test\.php - [L,NC]

更灵活的解决方案

下面的想法采用了不同的方法,并与您现有的白名单规则配合使用。如果您需要担心多个模式或比单个文件更复杂的东西,这可能会更好。

对于您列出的白色文件夹&#34;规则,添加条件

RewriteCond %{REQUEST_FILENAME} !/theme/test\.php

所以该块变为

##
## White listed folders
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/.well-known/*
RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/.*
RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
RewriteCond %{REQUEST_FILENAME} !/theme/test\.php
RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
RewriteRule !^index.php index.php [L,NC]

否则,标有&#34;阻止所有PHP文件的规则,除索引&#34;将路由到索引文件。

答案 1 :(得分:0)

兄弟,也许你可以试试这个:

RewriteRule /Route/to/Your.php

您的问题发生是因为您拒绝所有文件.php

幸运的是。