简体   繁体   中英

How to give access execute specific php file in server?

Hello guys first of all sorry for my bad english!. I'm having trouble with running a specific php file in server and I want to configure .htaccess to allow user to run the file. example: www.mywebsite.com/theme/test.php

this is my .htaccess file and when I check my site it return "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]

` Is there any way to fix the problem ?

Your problem is that the rule labeled "Block all PHP files, except index" will route all requests for PHP files to the index file. Here are two ways to fix this.

Simple, One-Off Solution

You can simply add this rule before any other RewriteRule lines:

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

More Flexible Solution

The idea below takes a different approach and works with your existing whitelisting rules. This might be better if you are goi to have multiple patterns or something more complex than a single file to worry about.

For your "white listed folders" rule, add the condition

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

So the block becomes

##
## 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]

Otherwise, the rule labeled "Block all PHP files, except index" will route to the index file.

Bro, maybe you can try with this:

RewriteRule /Route/to/Your.php

Your problem happens because you are denying all the files .php

Lucky with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM